1

下面的代碼是用於驗證,但爲了同樣我想檢查最新是否重複,如果是的話則給出說明記錄的錯誤信息已經存在。

$.validator.addMethod("region_name", function(value) {    
    if (value.trim().length >=2){ 
    return value; 
    } 
    }, 'At least two charachters Enter Region'); 
    validator = $('#region_form').validate({ 
       rules:{ 
        region_name:{ 
         required: true, 
         region_name: true//this is for error msg which we write in addMethod 
        }   
       },      
       highlight: function(element) { 
        $(element).closest('.form-group').addClass('has-error'); 
       }, 
       unhighlight: function(element) { 
        $(element).closest('.form-group').removeClass('has-error'); 
       }, 
       errorElement: 'span', 
       errorClass: 'help-block', 
       errorPlacement: function(error, element) { 
        if(element.parent('.input-group').length) { 
         error.insertAfter(element.parent()); 
        } else { 
         error.insertAfter(element); 
        } 
       } 
      }); 

回答

1

您可以使用remote method反對數據庫

rules:{ 
    region_name:{ 
     required: true, 
     region_name: true,//this is for error msg which we write in addMethod 
      remote: { 
      url: "check.php", 
      type: "post" 
      } 
    } 
}, 
messages: { 
    region_name: { 
     remote: "Define Remote Validation Message Here" 
    } 
}, 

檢查輸入值,而你並不需要編寫自定義的方法.addMethod檢查的輸入值長度,它也可以做到遵循

rules:{ 
    region_name:{ 
     required: true, 
     minlength: 2 //Minimum 2 charachters required 
    } 
}, 

Reference

+0

嗨感謝你的答覆,但我需要客戶端檢查和比較重複。我們能做到嗎? – eegloo