jQuery validation plugin: accept only alphabetical characters?

前端 未结 6 2283
-上瘾入骨i
-上瘾入骨i 2020-11-29 02:37

I\'d like to use jQuery\'s validation plugin to validate a field that only accepts alphabetical characters, but there doesn\'t seem to be a defined rule for it. I\'ve search

相关标签:
6条回答
  • 2020-11-29 02:51

    to allow letters ans spaces

    jQuery.validator.addMethod("lettersonly", function(value, element) {
    return this.optional(element) || /^[a-z\s]+$/i.test(value);
    }, "Only alphabetical characters");
    
    $('#yourform').validate({
            rules: {
                name_field: {
                    lettersonly: true
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-29 02:59

    Be careful,

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

    [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.

    For spaces + text, just do this:

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

    [a-z ] this allows spaces aswell as text only.

    ............................................................................

    also the message "Letters and spaces only please" is not required, if you already have a message in messages:

    messages:{
            firstname:{
            required: "Enter your first name",
            minlength: jQuery.format("Enter at least (2) characters"),
            maxlength:jQuery.format("First name too long more than (80) characters"),
            lettersonly:jQuery.format("letters only mate")
            },
    

    Adam

    0 讨论(0)
  • 2020-11-29 03:01

    Both Nick and Adam's answers work really well.

    I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:

    jQuery.validator.addMethod('lettersonly', function(value, element) {
        return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
    }, "Letters and spaces only please");
    
    0 讨论(0)
  • 2020-11-29 03:10

    Just a small addition to Nick's answer (which works great !) :

    If you want to allow spaces in between letters, for example , you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:

    jQuery.validator.addMethod("lettersonly", function(value, element) 
    {
    return this.optional(element) || /^[a-z," "]+$/i.test(value);
    }, "Letters and spaces only please");       
    
    0 讨论(0)
  • 2020-11-29 03:12

    If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

    You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.

    Here's an example:

    $("form").validate({
      rules: {
        myField: { lettersonly: true }
      }
    });
    

    It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:

    jQuery.validator.addMethod("lettersonly", function(value, element) {
      return this.optional(element) || /^[a-z]+$/i.test(value);
    }, "Letters only please"); 
    
    0 讨论(0)
  • 2020-11-29 03:14
     $('.AlphabetsOnly').keypress(function (e) {
            var regex = new RegExp(/^[a-zA-Z\s]+$/);
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }
            else {
                e.preventDefault();
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题