Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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.


Solution 2:

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

Solution 3:

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/


Post a Comment for "Regular Expression For Upper Case Letter Only In JavaScript"