jquery validate 自定义验证方法
jQuery.validator.addMethod("stringCheck", function (value, element) { //return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); if ($("#txtProductName").val().length < 10) { return false; } else { return true; } }, "请输入适合的长度"); //验证 function validateFrom() { $("#form1").validate({ rules: { txtProductName: { required: true, stringCheck: true }, txtMarketPrice: { required: true, number: true }, txtIntegral: { required: true, digits: true } }, messages: { txtProductName: { required: "请输入礼品名称" }, txtMarketPrice: { required: "请输入市场价" }, txtIntegral: { required: "请输入兑换积分" } } , success: function (label) { label.html(" ").attr("class", "success").siblings("label").remove(); }, errorPlacement: function (error, element) { $(element).next("span").find(".success").remove(); error.appendTo(element.next("span")); } }); }
自定义验证信息
jQuery.validator.addMethod("stringCheck", function (value, element) { //return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); if ($("#txtProductName").val().length < 10) { $.validator.messages["stringCheck"] = "错了"; return false; } else { $.validator.messages["stringCheck"] = "对了"; return true; } }); //验证 function validateFrom() { $("#form1").validate({ rules: { txtProductName: { required: true, stringCheck: true }, txtMarketPrice: { required: true, number: true }, txtIntegral: { required: true, digits: true } }, messages: { txtProductName: { required: "请输入礼品名称" }, txtMarketPrice: { required: "请输入市场价" }, txtIntegral: { required: "请输入兑换积分" } } , success: function (label) { label.html(" ").attr("class", "success").siblings("label").remove(); }, errorPlacement: function (error, element) { $(element).next("span").find(".success").remove(); error.appendTo(element.next("span")); } }); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。