0

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

1
  • Are you trying to prevent a user from entering numbers or is this a form that needs validation upon submit? Commented Dec 8, 2011 at 20:02

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, did not know about this plugin. Could come in handy. +1
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.