2013-03-23 20 views
5

Jquery'de özel doğrulama ile çalışmaya çalışıyorum. Tüm kodlama kısmı haklı, ama onun yanlış gittiği yere gitmiyorum ... işte kodun bir parçası.jQuery Doğrulama eklentisi ile özel yöntem

Password:<input type="password" id="pnameTxt" name="pnameTxt" placeholder="Enter Password" size=12 class='required'><br> 
Confirm Password:<input type="password" id="pnameTxt2" name="pnameTxt2" placeholder="Retype Password" size=15 class='required passwordCheck'><br> 

Özel doğrulama yöntemi:

$.validator.addMethod("passwordCheck",function (value,element){ 
      return value==$("#pnameTxt").val(); 

     }, 'Password and Confirm Password should be same'); 
+2

özel yöntemi yazmaya gerek yok. Sadece 'equalTo' kuralını kullanın. Bakınız: http://docs.jquery.com/Plugins/Validation/Methods/equalTo#other – Sparky

cevap

1

u doğru doğrulama eklentisi başlatıldı mı? html'yi form ve initialize the plugin içine koyduğumda beklendiği gibi çalışır.

<form id="test"> 
Password:<input type="password" id="pnameTxt" name="pnameTxt" placeholder="Enter Password" size=12 class='required'><br> 
Confirm Password:<input type="password" id="pnameTxt2" name="pnameTxt2" placeholder="Retype Password" size=15 class='required passwordCheck'><br> 
<input type="submit"> 
</form> 
$.validator.addMethod("passwordCheck",function (value,element){ 
     return value==$("#pnameTxt").val(); 

    }, 'Password and Confirm Password should be same'); 

$('#test').validate(); 
+0

nerede $ ('# test') yerleştirmeliyim validate(); –

+0

Formunuzdaki veya harici bir js dosyasına başvuruda bulunan aynı dosyadaki satır içi kod. Ve '$ (document) .ready (function() { // Kodunuz }); 'eklentiyi çağırdığınızda formun yüklü olduğundan emin olun. –

34

Kodunuz çalışıyor. Ayrıca, eklentiyi .validate() ile başlattığınızda, kuralı alanınıza atamanız gerekir.

Çalışma DEMO:


ANCAKhttp://jsfiddle.net/KrLkF/

$(document).ready(function() { 

    $.validator.addMethod("passwordCheck", function (value, element) { 
     return value == $("#pnameTxt").val(); 
    }, 'Password and Confirm Password should be same'); 

    $('#myform').validate({ // initialize the plugin 
     rules: { 
      pnameTxt2: { 
       passwordCheck: true 
      } 
     } 
    }); 

}); 
, bu işlev için özel bir yöntem yazmak gerekmez. JQuery Validate eklentisinin zaten bir equalTo rule vardır ve işte nasıl kullanılacağı.

Çalışma DEMO: http://jsfiddle.net/tdhHt/

$(document).ready(function() { 

    $('#myform').validate({ // initialize the plugin 
     rules: { 
      pnameTxt2: { 
       equalTo: "#pnameTxt" // using `id` of the other field 
      } 
     }, 
     messages: { 
      pnameTxt2: { 
       equalTo: "Password and Confirm Password should be same" 
      } 
     } 
    }); 

}); 
İlgili konular