對於每個規則,您可以使用自定義驗證規則以及您自己的自定義消息。
爲了簡單起見,下面是如何驗證「用戶名」輸入具有用於驗證三個自定義的方法(每個具有其自己的錯誤信息)的一個例子:
// Add three custom methods first:
$.validator.addMethod("nameCustom", function(value, element) {
return this.optional(element) || /^[a-zA-Z ]+/.test(value);
}, 'Please use english letters only.');
$.validator.addMethod("noSpaceStart", function(value, element) {
return value.indexOf(" ") != 0;
}, "Name cannot start with a space");
$.validator.addMethod("noSpaceEnd", function(value, element) {
return value.lastIndexOf(" ") != value.length - 1;
}, "Name cannot end with a space");
$('#form_to_validate').validate({
rules: {
username: {
// Add the custom validation methods to the username input
noSpaceStart: true,
noSpaceEnd: true,
nameCustom: true,
// required and maxlength are built-in methods in the plugin and are ready to be used
required: true,
maxlength: 50
}
}
});
它不工作號。它只在電子郵件中顯示該消息:「輸入電子郵件地址」。如我所說。我想在div容器中顯示味精? – Developer 2010-10-08 12:40:58
語法是正確的,這就是它應該完成的方式,所以問題必須在其他地方。 'validationRule'看起來像一個自定義驗證方法。究竟應該做什麼?你可以編輯你的問題,並添加該方法的代碼? – 2010-10-08 12:46:22
validationRule方法無所作爲。它只是返回true。好,如果我刪除該行。甚至比不符合兩條規則。以及如何將它顯示在id #response的Divcontainer中。我想在div容器中顯示消息 – Developer 2010-10-08 13:08:41