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")); } }); }