JavaScript - Validacion de formulario no me funciona

 
Vista:

Validacion de formulario no me funciona

Publicado por Ricardo J. Rios R. (12 intervenciones) el 21/11/2008 22:22:22
Buenas Tardes, gracias por leer mi nota, cualquier ayuda se agradece, tengo el siguiente problema, tengo un formulario donde no me esta validando los campos el javascript, no se si lo puse mal o no se que tengo mal, no se que hacer, les pego el codigo:

<!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=iso-8859-1" />
<title>Sistema de Inventario</title>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #339900;
background-image: url(Fondo.jpg);
background-repeat: repeat-x;
}
#Layer1 {
position:absolute;
left:11px;
top:338px;
width:208px;
height:39px;
z-index:1;
}
-->
</style>
</head>

<body>

<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var cedula = document.getElementById('cedula');
var nombre = document.getElementById('nombre');
var cargo = document.getElementById('cargo');


// Check each input in the order that it appears in the form!
if(isNumeric(cedula, "Por favor sólo ingrese números para la cédula") and notEmpty(cedula, "Por favor ingrese alguna cédula")){
if(isAlphabet(nombre, "Por favor sólo ingrese letras para su nombre") and notEmpty(cedula, "Por favor ingrese algun nombre")){
if(isAlphanumeric(cargo, "Por favor ingrese un cargo válido (sólo letras)") and notEmpty(cargo, "Por favor ingrese algun cargo")){
return true;
}
}
}


return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<div align="center">
<h1><strong>NUEVO USUARIO/RESPONSABLE </strong></h1>
</div>
<p> </p>
<form id="form1" name="form1" method="post" action="Responsable.php">
<label><strong>Cédula</strong>
<input name="cedula" type="text" id="cedula" />
</label>
<br/>
<br/> <br/>
<label><strong>Nombre</strong>
<input name="nombre" type="text" id="nombre" />
</label>
<br/> <br/>
<br/>
<label><strong>Cargo</strong>
<input name="cargo" type="text" id="cargo" />
</label>
<br/>
<br/> <br/>

<label>
<input type="submit" name="Submit" value="Enviar" onclick="notEmpty(document.getElementById('cedula'), 'Por favor ingrese una cédula valida')"
/>
</label>
<label>
<input type="reset" name="Submit2" value="Restablecer" />
</label>
</form>
<p> </p>
</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

RE:Validacion de formulario no me funciona

Publicado por weirdmix (185 intervenciones) el 25/11/2008 20:59:19
le quite la validacion del email, esta mal tu expresion regular, el principal error q encontre es q usaste el operador de otro lenguaje y no de visual basic, usaste -and- y en JS es doble ampersand (&&):
<!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=iso-8859-1" />
<title>Sistema de Inventario</title>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #339900;
background-image: url(Fondo.jpg);
background-repeat: repeat-x;
}
#Layer1 {
position:absolute;
left:11px;
top:338px;
width:208px;
height:39px;
z-index:1;
}
-->
</style>
</head>
<body>
<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var cedula = document.getElementById("cedula");
var nombre = document.getElementById("nombre");
var cargo = document.getElementById("cargo");

// Check each input in the order that it appears in the form!
if(isNumeric(cedula, "Por favor sólo ingrese números para la cédula") && notEmpty(cedula, "Por favor ingrese alguna cédula")){
if(isAlphabet(nombre, "Por favor sólo ingrese letras para su nombre") && notEmpty(cedula, "Por favor ingrese algun nombre")){
if(isAlphanumeric(cargo, "Por favor ingrese un cargo válido (sólo letras)") && notEmpty(cargo, "Por favor ingrese algun cargo")){
return true;
}
}
}

return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
/*var emailExp = /^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}*/
return true;
}
</script>
<div align="center">
<h1><strong>NUEVO USUARIO/RESPONSABLE </strong></h1>
</div>
<p> </p>
<form id="form1" name="form1" method="post" action="Responsable.php">
<label><strong>Cédula</strong>
<input name="cedula" type="text" id="cedula" />
</label>
<br/>
<br/>
<br/>
<label><strong>Nombre</strong>
<input name="nombre" type="text" id="nombre" />
</label>
<br/>
<br/>
<br/>
<label><strong>Cargo</strong>
<input name="cargo" type="text" id="cargo" />
</label>
<br/>
<br/>
<br/>
<label>
<input type="submit" name="Submit" value="Enviar" onclick="return formValidator();"/>
</label>
<label>
<input type="reset" name="Submit2" value="Restablecer" />
</label>
</form>
<p> </p>
</body>
</html>
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