JavaScript - Validar Formulario

 
Vista:

Validar Formulario

Publicado por Javier (2 intervenciones) el 17/04/2020 02:23:35
Buenas noches.
Hace mucho que no trabajo con JavaScript y quiero validar 2 campos de 1 formulario.

La cuestión es que estén llenos los campos o estén vacíos siempre me sale el mensaje que le puse de que tienen que llenar los campos. algo mal estoy haciendo en el código. Les copio y si me pueden ayudar les agradecería.

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
<!DOCTYPE html>
<html lang="es">
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>CAFIRA - Inicio Sesión Administrador</title>
  <link href= "https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
  <link rel="stylesheet" href="assets/css/style.css" />
 
<SCRIPT LANGUAGE="JavaScript">
 
function ValidaDatos() {
  var verificar=true;
  if (document.FrmLogInAdm.NombreUsuario.Value) {
  	verificar=false;
	} else if (!document.FrmLogInAdm.ClaveUsuario.Value){
  	verificar=false;
	}
 
  if (verificar){
	  document.FrmLogInAdm.submit();
	  }else{
		alert("Por favor, debe completar los datos necesarios.");
	}
}
 
</SCRIPT>
 
 
 </head>
<body>
 
  <p>LOGIN ADMINISTRADOR </p>
  <p>
<form  name="FrmLogInAdm"action="Login_Adm.php" method="post">
        <!--
            Nota: el atributo name es importante, pues lo vamos a recibir de esa manera
            en PHP
        -->
        <input name="NombreUsuario" type="text" placeholder="Escribe tu nombre de usuario">
        <br><br>
        <input name="ClaveUsuario" type="password" placeholder="Contraseña">
        <br><br>
        <!--Lo siguiente envía el formulario-->
        <input type="submit" value="Iniciar sesión" onClick="ValidaDatos();">
    </form>
    <p>&nbsp;</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
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Validar Formulario

Publicado por ScriptShow (692 intervenciones) el 17/04/2020 11:15:32
Saludos,

veamos una opción adaptable con facilidad:

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
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CAFIRA - Inicio Sesión Administrador</title>
<link href= "https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
<link rel="stylesheet" href="assets/css/style.css" />
<script type="text/javascript">
function ValidaDatos(){
var user=document.FrmLogInAdm.NombreUsuario.value;
var pass=document.FrmLogInAdm.ClaveUsuario.value;
 
if (user==null || user==""){
alert("Escribe un nombre de usuario válido.");
return false;
}else if (pass.length<6){
alert("Escribe tu Password de 6 caracteres.");
return false;
}
}
</script>
</head>
<body>
<p>LOGIN ADMINISTRADOR </p>
<p>
<form name="FrmLogInAdm"action="Login_Adm.php" method="post" onsubmit="return ValidaDatos()">
<!--
Nota: el atributo name es importante, pues lo vamos a recibir de esa manera
en PHP
-->
<input name="NombreUsuario" type="text" placeholder="Escribe tu nombre de usuario">
<br><br>
<input name="ClaveUsuario" type="password" placeholder="Contraseña">
<br><br>
<!--Lo siguiente envía el formulario-->
<input type="submit" value="Iniciar sesión">
</form>
<p>&nbsp;</p>
</body>
</html>

Espero sea útil.
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

Validar Formulario

Publicado por Javier (2 intervenciones) el 17/04/2020 13:44:33
Hola. Probé el código tal cual me lo pasaste y no me funciona.
De esta manera ni siquiera me envía el formulario. De la manera que hice yo me envía el formulario pero me aparece el mensaje de que están vacíos los campos antes de enviarlo.
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