Jquery:表单validation不起作用
我是Jquery的新手,希望你们能帮我解决这个jqueryvalidation问题。
一直在尝试validation表单但它根本不validation。 它接受我在字段中输入的任何内容,无论我设置了什么限制。
请帮忙。 谢谢。
这是我的代码:
$(function(){ $('#form').validate({ alert("bbbb"); rules: { name: { required: true, minlength: 2, maxlength: 20, lettersonly: true }, ssn: { required: true, minlength: 9, maxlength: 9, nowhitespace: true }, gender: { required: true }, mobile: { required: true, minlength: 10, maxlength: 13, digits: true }, address: { required: true, minlength: 10, }, email: { required: true, minlength: 6, email: true } }, messages: { name: { required: "Please enter your name", minlength: "Name should be more than 2 characters", maxlength: "Name should be less than 20 characters", lettersonly: "Name should contain only letters" }, ssn: { required: "Please enter your NRIC", minlength: "NRIC should be more than 9 characters", maxlength: "NRIC should be less than 9 characters", nowhitespace: "NRIC should not have any spaces" }, gender: { required: "Please select your gender", }, mobile: { required: "Please enter your mobile number", minlength: "Mobile number should be more than 10 characters", maxlength: "Mobile number should be less than 13 characters", digits: "Mobile number should contain only digits" }, address: { required: "Please enter your address", minlength: "Address should be more than 10 characters", }, email: { required: "Please enter your email address", minlength: "Password should be more than 6 characters", email: "Please enter a valid email address" } }, }); $("#submit").click(function(){ $("#form").submit(); return false; }); });
你有2个id="form"
元素,因此validation器被分配给div
而不是form
。
将div
的id
值更改为其他值。
此外,必须在query.validate.js
之后添加additional-methods.js
文件,并且由于validate
中的alert()
而存在语法错误,因此
然后
jQuery(function ($) { $('#form').validate({ rules: { name: { required: true, minlength: 2, maxlength: 20, lettersonly: true }, ssn: { required: true, minlength: 9, maxlength: 9, nowhitespace: true }, gender: { required: true }, mobile: { required: true, minlength: 10, maxlength: 13, digits: true }, address: { required: true, minlength: 10, }, email: { required: true, minlength: 6, email: true } }, messages: { name: { required: "Please enter your name", minlength: "Name should be more than 2 characters", maxlength: "Name should be less than 20 characters", lettersonly: "Name should contain only letters" }, ssn: { required: "Please enter your NRIC", minlength: "NRIC should be more than 9 characters", maxlength: "NRIC should be less than 9 characters", nowhitespace: "NRIC should not have any spaces" }, gender: { required: "Please select your gender", }, mobile: { required: "Please enter your mobile number", minlength: "Mobile number should be more than 10 characters", maxlength: "Mobile number should be less than 13 characters", digits: "Mobile number should contain only digits" }, address: { required: "Please enter your address", minlength: "Address should be more than 10 characters", }, email: { required: "Please enter your email address", minlength: "Password should be more than 6 characters", email: "Please enter a valid email address" } }, }); });
演示: 小提琴
嘿,你需要像这样在库中创建自定义规则
$.validator.addMethod("valueNotEquals", function(value, element, arg){ return arg != value; }
然后像这样使用它
rules: { gender: {valueNotEquals: "default"} }, messages: { gender: { valueNotEquals: "Please select a payment method"} }