JavaScript - estandarizar mi funcion

 
Vista:
sin imagen de perfil

estandarizar mi funcion

Publicado por Santon (3 intervenciones) el 11/11/2014 19:34:10
tengo esta funcion Javascritp

1
2
3
4
5
6
7
8
//FUNCION PARA VALIDAR CARACTERES NO PERMITIDOS		
function validarString () {
   for (var i = 0; i< document.form1.txt_campo1.value.length; i++) {
         var caracter = document.form1.txt_campo1.value.charAt(i);
         if( caracter == "%" || caracter == "'") {
            alert("EL Campo contiene un caracter no permitido, Verifique");
			document.form1.txt_campo1.value = "";
}}}

con esta funcion valido que en la caja de texto no ingresen el caracter % y el caracter ' por ejemplo

pero mi detalle es que tengo 3 campos de textos

campo 1

campo 2

campo 3

con esto tendria que crear una funcion validarString para cada uno y yo quiziera crear solo una funcion y estandarizarla para que los tres campos se validen en la misma funcion

el codigo completo es este

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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
 
//FUNCION PARA VALIDAR CARACTERES NO PERMITIDOS		
function validarString () {
   for (var i = 0; i< document.form1.txt_apellidos.value.length; i++) {
         var caracter = document.form1.txt_apellidos.value.charAt(i);
         if( caracter == "%" || caracter == "'") {
            alert("EL Campo contiene un caracter no permitido, Verifique");
			document.form1.txt_apellidos.value = "";
}}}
</script>
<body>
<form action="" method="post" name="form1" id="form1"  enctype="multipart/form-data">
<label>Campo 1
<input type="text" name="txt_campo1" id="txt_campo1" onBlur="validarString()" />
</label>
<p>
  <label>Campo 2
  <input type="text" name="txt_campo2" id="txt_campo2" onBlur="validarString()"/>
  </label>
</p>
<p>
  <label>Campo 3
  <input type="text" name="txt_campo3" id="txt_campo3" onBlur="validarString()"/>
  </label>
</p>
</form>
 
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

estandarizar mi funcion

Publicado por xve (2100 intervenciones) el 12/11/2014 14:25:09
Hola Santon, la manera que se me ocurre, es pasando el id de cada <input>, de esta manera, siempre trabas con el id... aqui te muestro el ejemplo:

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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
 
//FUNCION PARA VALIDAR CARACTERES NO PERMITIDOS     
function validarString (id)
{
    var contenido=document.getElementById(id).value;
    for (var i = 0; i<contenido.length; i++)
    {
        var caracter = contenido.charAt(i);
        if( caracter == "%" || caracter == "'")
        {
            alert("EL Campo contiene un caracter no permitido, Verifique");
            document.getElementById(id).value = "";
        }
    }
}
</script>
<body>
<form action="" method="post" name="form1" id="form1"  enctype="multipart/form-data">
<label>Campo 1
<input type="text" name="txt_campo1" id="txt_campo1" onBlur="validarString(this.id)" />
</label>
<p>
  <label>Campo 2
  <input type="text" name="txt_campo2" id="txt_campo2" onBlur="validarString(this.id)"/>
  </label>
</p>
<p>
  <label>Campo 3
  <input type="text" name="txt_campo3" id="txt_campo3" onBlur="validarString(this.id)"/>
  </label>
</p>
</form>
 
</body>
</html>

Coméntanos si te sirve, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

estandarizar mi funcion

Publicado por Santon (3 intervenciones) el 12/11/2014 22:33:18
Era exactamente lo que buscaba Eres grande xve, funciono al 100%, mil gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar