2010-03-19 36 views

Trả lời

36

Chỉ cần thêm một validator tùy chỉnh, và sử dụng nó như thế này:

jQuery.validator.addMethod("accept", function(value, element, param) { 
    return value.match(new RegExp("." + param + "$")); 
}); 

Chỉ số:

rules: { 
    field: { accept: "[0-9]+" } 
} 

Chỉ có chữ cái

rules: { 
    field: { accept: "[a-zA-Z]+" } 
} 
+0

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 –

+2

Không nên là '.' là'^'? –

+0

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

18

Một sự thay đổi nhỏ.

jQuery.validator.addMethod("accept", function(value, element, param) { 
    return value.match(new RegExp("^" + param + "$")); 
}); 

Vì cách đó nó chấp nhận các biểu thức như "#abc".

0

Hãy thử như thế này:

 var numbers = /[0-9]+/; 
     var SpCharacters = /^\s*[a-zA-Z0-9\s]+\s*$/; 

     if (!numbers.test(name) && !SpCharacters.test(name)) { 
      return [false, "Name should be alphabetical.", ""]; 
     } 
4

Sử dụng dưới mã trong kịch bản của bạn. điều này sẽ thêm mệnh đề mới vào trình xác thực của bạn.

$.validator.addMethod(
     "alphabetsOnly", 
     function(value, element, regexp) { 
      var re = new RegExp(regexp); 
      return this.optional(element) || re.test(value); 
     }, 
     "Please check your input values again!!!." 
); 
$("#formElement").rules("add", {alphabetsOnly: "^[a-zA-Z'.\\s]$" }) 
0

Phương pháp sau để thêm trình xác thực tùy chỉnh hoạt động tốt, Tuy nhiên, quá trình xác thực xảy ra trên khóa, Vì vậy, lỗi tiếp tục xuất hiện khi người dùng nhập văn bản.

return value.match(new RegExp("")); 

Các phương pháp dưới đây hoạt động tốt cho bảng chữ cái và không gian, lý tưởng cho các lĩnh vực tên.

jQuery.validator.addMethod("alphabetsAndSpacesOnly", function (value, element) { 
    return this.optional(element) || /^[a-zA-Z\s]+$/.test(value); }); 

    $("FieldId").rules("add", { 
     alphabetsAndSpacesOnly: true, 
     messages: { alphabetsAndSpacesOnly: "Please enter a valid name." } 
    }); 
0

Plugin jQuery Validate đã có một quy tắc gọi là lettersonly như một phần của the additional-methods.js file, trông như thế này ...

$.validator.addMethod("lettersonly", function(value, element) { 
    return this.optional(element) || /^[a-z]+$/i.test(value); 
}, "Letters only please"); 
0

Thêm cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/additional-methods.js"></script> 
rules:{ 
    txtname:{required: true, 
      lettersonly:true 
      } 
0

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" 
       }, 
      } 
    }); 
}); 
Các vấn đề liên quan