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> </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>
<td align="right"> Last name :</td>
<td><input type="text" name="lname" class="textbox" id="lname" placeholder="Required field"/><div class="myErrors"></div></td>
</tr>
<tr>
<td> </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 :</td>
<td width="167" id="showPP"> <input type="text" name="passport" class="textbox" id="ppnu" placeholder="Required field"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="3"><input type="submit" name="submit" value="Submit "class="submit" id="submit" />
<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
}
});
});
Đâu là đánh dấu HTML cho hình thức này? – Sparky
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
@ Mã html Sparky hơi dài để thêm vào đây – Erandi