validation works fine with the form and validation plugin as static form. i need to insert the form into the page and then have it validate – not working for me.
$(document).ready(function(){
var validator = $("#loginForm").validate({
// rules for field names
rules: {
cname: "required",
cpass: "required"
},
// inline error messages for fields above
messages: {
cname: "required",
cpass: "required"
}
}); // for validating the form
$("#loginForm").validate({
submitHandler: function(form) { //Only runs when valid
form.submit();
}
});
var htmlStr = '<form class="cmxform" id="loginForm" method="get" action="https://www.myURL.com/login.php"><p><label for="cname">Name</label><em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" /></p><p><label for="cpass">Password</label><em>*</em><input type="password" id="cpass" name="pass" size="25" class="required" minlength="2" /></p><input id="mySubmit" class="submit" type="submit" value="Submit"/></form>';
function createForm(){
// fires with body onload
$("#login-container").html(htmlStr);
};
Move your validation code to right after you add the html to the page. Since
$("#loginForm")likely doesn’t exist in yourdocument.readyfunction, nothing in thevalidatearea ever gets run. You probably want your code to look like this:Other problems you had – no need to submit the form manually in
submitHandler– the validator will do that for you if the form is valid. Also, with the way you’ve set up your messages object, the actual error message will be the word “required”. Not sure if that’s what you want or not…