Thử Điều này: (Đây là xác nhận tùy chỉnh hoàn hảo bằng cách sử dụng jquery validator)
$(function() {
$.validator.addMethod("alphabetsnspace", function(value, element) {
return this.optional(element) || /^[a-zA-Z ]*$/.test(value);
});
$("#add-employee").validate({
rules: {
employee_surname: {
required: true,
alphabetsnspace: true
},
employee_firstname: {
required: true,
alphabetsnspace: true
},
employee_othername: {
alphabetsnspace: true
},
father_name: {
required: true,
alphabetsnspace: true
},
mother_name: {
required: true,
alphabetsnspace: true
},
spouse_name: {
alphabetsnspace: true
},
ssn: {
number: true,
},
phone_no: {
number: true,
},
phone_no2: {
number: true,
},
passport: {
number: true,
},
driving_license: {
number: true,
},
email: {
email: true
}
},
messages:
{
"employee_surname":{
alphabetsnspace: "Please Enter Only Letters"
},
"employee_firstname":{
alphabetsnspace: "Please Enter Only Letters"
},
"employee_othername":{
alphabetsnspace: "Please Enter Only Letters"
},
"father_name":{
alphabetsnspace: "Please Enter Only Letters"
},
"mother_name":{
alphabetsnspace: "Please Enter Only Letters"
},
"spouse_name":{
alphabetsnspace: "Please Enter Only Letters"
},
}
});
});
Nguồn
2017-11-09 06:36:43
chấp nhận là phương pháp gốc để xác thực phần mở rộng tệp. http://docs.jquery.com/Plugins/Validation/Methods/accept#extension –
Không nên là '.' là'^'? –
Nhân tiện, việc tạo quy tắc mới là không cần thiết vì plugin đã có quy tắc được gọi là 'lettersonly' như một phần của [tệp' additional-methods.js'] (https://cdnjs.cloudflare.com/ajax/libs /jquery-validate/1.15.0/additional-methods.js). – Sparky