Belgarion
Belgarion

Reputation: 159

jQuery validation for confirming password not working

I was looking at the following question: Password confirmation trouble with jQuery validation plugin

I copied the code on jsfiddle and edited it accordingly to make it run on my server, but it doesn't seem to be working and I can't figure it out! Kindly help, thanks!

Here's my code.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>        
<script>
$.validator.setDefaults({
    submitHandler: function() {
        alert("submitted!");
    }
});

// validate signup form on keyup and submit
$("form").validate({
    rules: {
        pass: {
            required: true,
            minlength: 5
        },
        pass2: {
            required: true,
            minlength: 5,
            equalTo: "#pass"
        }
    },
    messages: {
        pass: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        pass2: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        }
    }
});
</script>

</head>
<body>
<form>
<table>
    <tr>
        <td>Password</td>
        <td>
            <label for="pass"></label>
            <input id="pass" name="pass" type="password" value="" />
        </td>
    </tr>
    <tr>
        <td>Password confirm</td>
        <td>
            <label for="pass2"></label>
            <input id="pass2" name="pass2" type="password" value="" />
        </td>
    </tr>
</table>
<input type="submit" name="save" value="Save" />
</form>
</body>
</html>

Upvotes: 1

Views: 11587

Answers (1)

kevhann80
kevhann80

Reputation: 311

Try wrapping your JS this function

$(function () {
$.validator.setDefaults({
    submitHandler: function() {
        alert("submitted!");
    }
});

// validate signup form on keyup and submit
$("form").validate({
    rules: {
        pass: {
            required: true,
            minlength: 5
        },
        pass2: {
            required: true,
            minlength: 5,
            equalTo: "#pass"
        }
    },
    messages: {
        pass: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        pass2: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        }
    }
});
 });

Upvotes: 4

Related Questions