简体   繁体   中英

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 AZ characters only.

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.

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM