//自定义规则验证 $.validator.addMethod("onlycode", function (value, element, params) { var re = new RegExp(params); if ($.trim(value).length < 0 || $.trim(value) == "") return true; if (re.test(value)) { return true; } else { return false; } }, ""); //设置验证 $("#myform").validate({ rules: {
【注: selectName,selectTel,selectEmail 是属性name】 SelectName: { required: true, minlength: 2, maxlength: 64 }, SelectTel: { required: true, minlength: 2, maxlength: 20, onlycode: /^[0-9]*$/ }, SelectEmail: { required: true, minlength: 2, maxlength: 20, email:true}, }, messages: { SelectName: { required: "不能为空", maxlength: jQuery.format("不能超过{0}个字符") }, SelectEmail: { required: "不能为空", maxlength: jQuery.format("不能超过{0}个字符"),email:jQuery.format("必须输入正确的电邮")} }, submitHandler: function (form) { form.submit(); } });
//提交验证 if ($("#myform").validate().form()) { alert("验证成功"); return; } else { alert("验证失败"); return; }