nsdiv
nsdiv

Reputation: 983

JQuery validation of non numeric field

How do I validate a field to have only non numeric characters? I have a field for first name, which can only Use a-z A-Z and allow '-', whitespace between characters. I tried using firstName: { required: true, minlength: 2, maxlength: 32, digits: false },

But the digits rule just doesn't work when it's set to false

Upvotes: 0

Views: 1297

Answers (2)

The Maniac
The Maniac

Reputation: 2626

You can use jQuery's filter function to filter out all of your inputs with values that don't match any regex. In the example below, I've added a class ('my-error-class') to all of the inputs that didn't match. You could do anything here. Also, I'm no regex pro, so I don't know if the dash inside the brackets needs to be escaped.

$('.some-inputs')
    .filter(function(index) {
        return !this.value.match(/[a-zA-Z\-\s]+/);
    })
    .addClass('my-error-class');

Upvotes: 0

Rondel
Rondel

Reputation: 4951

If you want to have the input masked so that they can't enter numbers at all, you can use this jQuery plugin http://hdserv.me/jQuery/Samples/jQuery%20AlphaNumeric.html. Then you can use

$('.sample3').alpha()

You can set the allow property to allow '-'. There are a bunch of examples on the site

Upvotes: 1

Related Questions