My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contar caracteres on-line</title>

<script type="text/javascript">
function WordCountOnLine() {

// Función que nos va a contar el número de caracteres
// y de palabras del area de texto de un formulario
// Obtenemos el texto del area.
textoArea = document.getElementById("area_texto").value;

// El numero de caracteres es su longitud
numeroCaracteres = textoArea.length;

// Eliminamos los caracteres en blanco del inicio y del final.
// Como no tenemos funciones del tipo trim, rtrim y ltrim usamos
// expresiones regulares
inicioBlanco = /^ / // El ^ indica principio de cadena
finBlanco = / $/ // El $ indica final de cadena


// El global (g) es para obtener todas las posibles combinaciones
variosBlancos = /[ ]+/g
textoArea = textoArea.replace(inicioBlanco,"");
textoArea = textoArea.replace(finBlanco,"");
textoArea = textoArea.replace(variosBlancos," ");

// Creamos un array con las diferentes palabras.
// Teniendo en cuenta que la separación
// entre palabras es el espacio en blanco.
textoAreaDividido = textoArea.split(" ");
numeroPalabras = textoAreaDividido.length;

document.getElementById("caracteres").value = numeroCaracteres;
document.getElementById("palabras").value = numeroPalabras;
setTimeout("WordCountOnLine();",300);
}
</script>
</head>
<body onLoad="WordCountOnLine();">

<h1>Contar caracteres on-line</h1>

<form name="formulario">
<label for="area_texto">Empiece a escribir...</label><br>
<textarea id="area_texto" style="font-family:verdana;font-size:12px;" rows=10 cols=50></textarea><br>
<label for="caracteres">Número de caracteres:</label>
<input id="caracteres" style="font-family:verdana;font-size:12px;" type="text" size=4 value="0">
<label for="palabras">Número de palabras:</label>
<input id="palabras" style="font-family:verdana;font-size:12px;" type="text" size=4 value="0">
</form>

<br><br>
<hr>
Art&iacute;culo disponible en: <a href="http://lineadecodigo.com/2008/03/25/contar-caracteres-on-line/">http://lineadecodigo.com/2008/03/25/contar-caracteres-on-line/</a><br/>
<a href="http://lineadecodigo.com" title="Linea de Codigo">lineadecodigo.com</a>

</body>
</html>

Change log

r45 by vcuervo on Mar 24, 2008   Diff
Cambio en valores por defecto.
Go to: 
Project members, sign in to write a code review

Older revisions

r44 by vcuervo on Mar 24, 2008   Diff
Contar caracteres on-line
All revisions of this file

File info

Size: 2420 bytes, 62 lines
Powered by Google Project Hosting