I’m using jQuery Validate with jQuery Mobile for a mobile application. I’ve been trying to get this work for ages and cannot see what’s wrong. Essentially the form submits without triggering any of the validation.
I’ve backtracked a good bit and copied the version of jquery and the validate.js from the sample page at http://jquery.bassistance.de/validate/demo/. Still doesn’t work 🙁
<script type="text/javascript">
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
});
$().ready(function() {
// validate the comment form when it is submitted
//$("#commentForm").validate();
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 5
},
password2: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true
},
},
messages: {
username: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
password2: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
},
email: "Please enter a valid email address"
}
});
</script>
The form is being initialised in PHP:
echo "<form id=\"signupForm\" action='".$_SERVER['PHP_SELF']."' method=\"post\">";
Well your js code seems to be invalid, Here’s the valid code