Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 544
Rohan Kumar Avatar asked Dec 07 '11 07:12

Rohan Kumar


People also ask

How do you uppercase in regular expressions?

This can be done easily using regular expressions. In a substitute command, place \U or \L before backreferences for the desired output. Everything after \U , stopping at \E or \e , is converted to uppercase. Similarly, everything after \L , stopping at \E or \e , is converted to lowercase.

What is A+ in regular expression?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus . Regular Expression.


1 Answers

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.

like image 119
flesk Avatar answered Oct 06 '22 00:10

flesk