Validate a text field within two div

2

How could you validate a form?

I already tried this and nothing.

HTML

<form class="login-form">
    <h1>Login</h1>
    <br>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Usuario" name="User">

    </div>
    <div class="form-group log-Pass">
        <input type="password" class="form-control" placeholder="Contraseña" name="Password">
    </div>
    <span class="alert">Datos Erroneos</span>
    <a class="link" href="#">¿Olvidaste tu contraseña?</a>
    <button type="button" class="log-btn" onClick="login()">Entrar</button>
</form>

JavaScript

function login() {
    var usuario = document.login - form.User.value;
    var contraseña = document.login - form.Password.value;
    if (usuario == "root" && contraseña = "123") {
        alert("Logeado");
    } else {
        alert("Datos erroneos");
    }
}
    
asked by Axel Arteaga 15.08.2016 в 04:16
source

3 answers

2

In if (usuario == "root" && contraseña = "123") you are assigning the password to 123 instead of 123 ===

    
answered by 15.08.2016 / 04:40
source
2

Why are you trying to validate data in the front-end? , it is better to do it from the back-end, the error is, in that you are assigning instead of comparing.

    
answered by 15.08.2016 в 05:23
0

The problem is because of the letter ñ since javascript does not find it part of a variable identifier to accept you put this code in the meta and also give the form a name,

This is your example and if it works for me

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title></title>
    <script   src="https://code.jquery.com/jquery-3.1.0.js"   integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="   crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">

</script>
</head>
<body>

<form class="form" action="index.php" method="POST" name="form">
    <h1>Login</h1>
    <br>
    <div class="form-group">
        <input name="user" type="text" class="form-control" placeholder="Usuario" >

    </div>
    <div class="form-group log-Pass">
        <input name="password" type="password" class="form-control" placeholder="Contraseña" >
    </div>
    <span class="alert">Datos Erroneos</span>
    <a class="link" href="#">¿Olvidaste tu contraseña?</a>
    <button type="button" class="log-btn" onClick="login()">Entrar</button>
<script type="text/javascript">
    function login() {
    var usuario = document.form.user.value;
    var contraseña = document.form.password.value;
        if (usuario == "root" && contraseña == "123") {
        alert("Logeado");
    } else {
        alert("Datos erroneos");
    }
    }
</script>

</form>

</body>
</html>

    
answered by 15.08.2016 в 05:22