Rohan Kumar
Rohan Kumar

Reputation: 40639

Regular expression for upper case letter only in JavaScript

How can I validate a field only with upper case letters which are alphabetic. So, I want to match any word made of A-Z characters only.

Upvotes: 8

Views: 50152

Answers (3)

Irvin Dominin
Irvin Dominin

Reputation: 30993

Try this:

if (<value>.match(/^[A-Z]*$/)) {
    // action if is all uppercase (no lower case and number)
} else {
    // else
}

here is a fiddle: http://jsfiddle.net/WWhLD/

Upvotes: 1

flesk
flesk

Reputation: 7579

Try something like this for the javascript validation:

if (value.match(/^[A-Z]*$/)) {
    // matches
} else {
    // doesn't match
}

And for validation on the server side in php:

if (preg_match("/^[A-Z]*$/", $value)) {
    // matches
} else {
    // doesn't match
}

It's always a good idea to do an additional server side check, since javascript checks can be easily bypassed.

Upvotes: 15

Paul
Paul

Reputation: 141829

var str = 'ALPHA';
if(/^[A-Z]*$/.test(str))
    alert('Passed');
else
    alert('Failed');

Upvotes: 5

Related Questions