jQuery validation plugin: accept only alphabetical characters?

前端 未结 6 2292
-上瘾入骨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条回答
  •  Happy的楠姐
    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");       
    

提交回复
热议问题