2014-05-14 16 views
13

Tôi muốn xác thực một hộp văn bản bằng cách sử dụng phương thức xác thực tùy chỉnh của jQuery Validate plugin (addMethod) nhưng mã của tôi không hoạt động. Bất cứ ai có thể giúp tôi để tìm lỗi? Tôi chưa bao giờ sử dụng phương pháp xác nhận tùy chỉnh trước đây vì vậy nó hơi khó cho tôi để tìm nơi tôi đã đi sai trong mã này.Sử dụng addMethod trong jQuery Validate plugin

Đây là mã của tôi:

$(document).ready(function() { 

jQuery.validator.setDefaults({ 
    // where to display the error relative to the element 
    errorPlacement: function(error, element) { 
     error.appendTo(element.parent().find('div.myErrors')); 
    } 
}); 

jQuery.validator.addMethod(
"selectnic" 
function(value,element){ 
if(element.value == /^[0-9]{9}[vVxX]$/) 
    return false; 
    else return true; 
}, 
"wrong nic number" 
); 



    $('#basicDetails').validate({ // initialize the Plugin 
     rules: { 
       fname: { 
        required: true, 
        lettersonly: true, 
       }, 
       lname: { 
        required: true, 
        lettersonly: true, 
       }, 
      }, 
     messages: { 
      fname: { 
       required:"Please enter your first name", 
       lettersonly: "Login format not valid", 

      }, 
      lname: { 
       required:"Please enter your last name", 
       lettersonly: "Login format not valid", 
      }, 
     }, 
     submitHandler: function (form) { 
      alert('valid form submitted'); 
      return false; 
     } 
    }); 

}); 

đang ..html ....

<form action="#" method="post" id="basicDetails" runat="server"> 

<table width="68%" border="0" cellpadding="6" cellspacing="6"> 


<tr> 
            <td>&nbsp;</td> 
            <td> First name </td> 
            <td>:</td> 
            <td><input type="text" name="fname" id="fname" class="textbox" placeholder="Required field"/><div class="myErrors"></div></td> &nbsp; 
            <td align="right"> Last name&nbsp; :</td> 
            <td><input type="text" name="lname" class="textbox" id="lname" placeholder="Required field"/><div class="myErrors"></div></td> 
           </tr> 

           <tr> 
            <td>&nbsp;</td> 
            <td width="147" > NIC no </td> 
            <td> : </td> 
            <td width="172"><input type="text" name="nic" id="nic" class="textbox" placeholder="Required field"/><div class="myErrors"></div></td> 
            <td width="167" align="right">Passport no &nbsp; :</td> 
            <td width="167" id="showPP"> <input type="text" name="passport" class="textbox" id="ppnu" placeholder="Required field"/></td>  
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
            <td colspan="3"><input type="submit" name="submit" value="Submit "class="submit" id="submit" />&nbsp; &nbsp; 
            <input type="submit" name="reset" value="Reset "class="reset" /> 
            </td>         
           </tr> 


</table> 





</form> 

..... sau khi chỉnh sửa mã của tôi ....

$(document).ready(function() { 



jQuery.validator.setDefaults({ 
    // where to display the error relative to the element 
    errorPlacement: function(error, element) { 
     error.appendTo(element.parent().find('div.myErrors')); 
    } 
}); 

jQuery.validator.addMethod("selectnic", function(value, element){ 
    if (/^[0-9]{9}[vVxX]$/.test(value)) { 
     return false; 
    } else { 
     return true; 
    }; 
}, "wrong nic number"); 


    $('#basicDetails').validate({ // initialize the Plugin 


     rules: { 
       fname: { 
        required: true, 
        lettersonly: true, 
         }, 
       lname: { 
        required: true, 
        lettersonly: true, 
        }, 

       nicnumber: { 
          // other rules, 
         selectnic: true // <- declare the rule someplace! 
          } 


      }, 


     messages: { 

      fname: { 
       required:"Please enter your first name", 
       lettersonly: "Login format not valid", 

      }, 
      lname: { 
       required:"Please enter your last name", 
       lettersonly: "Login format not valid", 

      }, 
     }, 


     submitHandler: function (form) { // for demo 
      alert('valid form submitted'); // for demo 
      return false; // for demo 
     } 
    }); 

}); 
+0

Đâu là đánh dấu HTML cho hình thức này? – Sparky

+1

Tôi có thể thấy lỗi cú pháp, bạn đã bỏ lỡ ',' sau 'selectnic'. Ngoài ra, điểm so sánh một chuỗi với regex là gì? – undefined

+0

@ Mã html Sparky hơi dài để thêm vào đây – Erandi

Trả lời

31

Có ba vấn đề khác nhau như được nêu trong các chỉnh sửa đang tiến hành bên dưới.


1) Bạn bỏ lỡ một dấu phẩy chỉ giữa selectnicfunction(.

Ngoài ra, hãy thử định dạng này để thay thế khi sử dụng regex ...

jQuery.validator.addMethod("selectnic", function(value, element){ 
    if (/^[0-9]{9}[vVxX]$/.test(value)) { 
     return false; // FAIL validation when REGEX matches 
    } else { 
     return true; // PASS validation otherwise 
    }; 
}, "wrong nic number"); 

EDIT: Câu trả lời này giả định ban đầu regex và logic của bạn đều đúng. Logic của bạn trả về false khi số regex phù hợp. false có nghĩa là xác thực không thành công và bạn sẽ thấy thông báo lỗi.


2) EDIT 2:

Sau khi tạo phương pháp mới, bạn cũng phải sử dụng họ. Tôi không thấy quy tắc/phương pháp của selectnic được sử dụng bất kỳ nơi nào trong mã của bạn.

Ví dụ:

rules: { 
    myFieldName: { 
     // other rules, 
     selectnic: true // <- declare the rule someplace! 
    } 
} 

3) EDIT 3:

Và cuối cùng, ban đầu true/false logic của OP là ngược. Ông muốn PASS xác nhận khi một trận đấu regex ... do đó, cần phải return true.

if (/^[0-9]{9}[vVxX]$/.test(value)) { 
     return true; // PASS validation when REGEX matches 
    } else { 
     return false; // FAIL validation 
    }; 

DEMO: http://jsfiddle.net/DzvNr/

+0

@ sparky this does not work :( – Erandi

+0

@Erandi, chỉ cần nói "it didn ' t làm việc "vô cùng vô ích với mọi người. Điều gì đã không làm việc? Bạn đã thực hiện khắc phục sự cố gì? Lỗi nào đang hiển thị trong bảng điều khiển của bạn? ** Bạn có chắc chắn _your_ regex là đúng không? Là _your_ logic đúng không? Chức năng của bạn sẽ trả về' false '(FAIL validation) khi' regex' IS trùng khớp. ** – Sparky

+0

@ Sparky regex là chính xác ở đây những gì tôi muốn là khi người dùng nhập số NIC nó kiểm tra xem số đã nhập có định dạng '804567789V' (9 số và chữ cái cuối cùng không là 'x' hoặc 'y') và sử dụng plugin xác thực jQuery bình thường Tôi không thể làm điều này vì vậy tôi đã sử dụng phương thức xác thực tùy chỉnh nhưng ở đây sau khi thêm mã này tôi đã thử nhưng không hiển thị bất kỳ thông báo lỗi nào – Erandi

Các vấn đề liên quan