vendredi 17 juillet 2020

In form works only first if or both if

i need help, I am new to js. I try to create a form that shows error when one of the fields or both is empty The problem is that it shows the error when both fields empty or field name is empty, but when i write name and don't write password its don't show error, that field password is empty. Sorry for my English it's not my native language.I don't understand why?

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        function valid()
        {
            var fname = document.form.name.value;
            var fpass = document.form.password.value;
            var err = "";
            var ret = true;

            if(fname == "" || fname == NULL)
            {
              err += "Need name.\n";
            } 

            if( fpass == "" || fpass == NULL)
            {
                err += "Need password.!_!";
            }


            (err != "") ? alert(err) : ret = false;
              
            return ret;                
        }
    </script>
        <form name="form" action="" method="post" onsubmit="return valid()">
            Your Name: <input type="name" name="name"><br><br>
            Your Password: <input type="password" name="password"><br>
            <input type="submit" value="Submit">
            
        </form>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire