Robin Michael Poothurai
Robin Michael Poothurai

Reputation: 5664

jquery validation should allow alphabets

Can anyone please tell me jquery validation, that should validate only alphabets, i.e)

abcde - valid
12344  - not valid
ancd12 - not valid
asng$e  - not valid etc

this code will not work for above conditions.

alphabet: function(value, element, param) {
    return this.optional(element) || value.match(/[a-z]/gi);
}

Upvotes: 1

Views: 1350

Answers (2)

politus
politus

Reputation: 6086

use validation plugin method written specifically for this purpose, found in the validation plugin additional-methods.js

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

Upvotes: 1

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

alphabet: function(value, element, param) {
    return /^[A-z]+$/.test(value);
}

Upvotes: 1

Related Questions