Regular Expression

I need this to allow uppercase caps only, with a blank entry required.  

"^[A-Z]{0,1}"

But it seems to give error if the entry field is blank.  

Any suggestions on this would be welcome

Thanks!
LVL 2
codequestAsked:
Who is Participating?
 
stanscott2Connect With a Mentor Commented:
Do it like this:

var str = "";
alert(str.match(/^([A-Z]+)$/)==null);

A true condition means that either a non-caps character is present, or the string is empty.
0
 
netsmithcentralConnect With a Mentor Commented:
Not sure exactly what you need, so here's some for you:

Allow any number of capital letters (or a blank field):
"^[A-Z]*$"

Allow any number of capital letters, no blank field:
"^[A-Z]+$"

Allow exactly one capital letter (or a blank):
"^[A-Z]?$"

Allow exactly one capital letter, no blank field:
"^[A-Z]$"


These last two are NOT available in all flavors of regular expressions.  Javascript allows them, others may or may not.
Allow x capital letters, (or a blank field):
"^[A-Z]{0,x}$"

Allow x capital letters, no blank field:
"^[A-Z]{1,x}$"
0
 
codequestAuthor Commented:
Thanks for inputs, that should do it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.