vendredi 9 mars 2018

Trouble with JavaScript Validation on Form

Trying to validate some input for a login form using JavaScript. From what I can see, everything in the script is correct, but it's not working. I'm sure it's a tiny thing that I've missed, any help would be appreciated.

JavaScript code:

<script>
    function validateForm() {
        var x = document.forms["login"]["username"].value;
        var y = document.forms["login"]["password"].value;
        if (isNaN(x)) && (y === "") {
            alert("Username must be numerical, and password must be entered!");
            return false;
        } else if (isNaN(x)) && (y !== "") {
            alert("Username must be numerical!");
            return false;
        } else if (Number.isInteger(x) == true) && (y === "") {
            alert("Password must be entered!");
            return false;
        } else {
            return true;
        }
    }
</script>

HTML Code:

<form name="login" method="get">
            <input type="text" placeholder="Customer ID" name="username">
            <input type="password" placeholder="Password" name="password">
            <input type="submit" onclick="return validateForm()" value="Log In" name="button">
            <?php if (isset($errormessage)) { echo $errormessage; }
            ?>
</form>

(I'm very new to web dev, please don't judge too much :p)

Aucun commentaire:

Enregistrer un commentaire