验证与表单和验证插件作为静态表单一起工作。我需要将表单插入页面,然后对其进行验证 - 不适合我。
$(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);
};