I need a Regular Expression that can validate an exact 3 character(alpha only) code but also a blank field to set as the validation expression of a ASP.NET RegEx validator control.
I am currently using ^[a-zA-Z]{3}$
and this works out well to match the code but of course doesn't match a blank.
I have been looking at using something like this:
^(?:|)[a-zA-Z]{3}$
-
1Where a blank field means any amount of white space, or nothing entered at all?Shane Wealti– Shane Wealti2011-07-14 19:15:35 +00:00Commented Jul 14, 2011 at 19:15
-
1Sorry for the confusion, it should allow blank with no white space, which according to @Ahmad Mageed I do not need to change the initial RegEx since it won't check the blank fieldErik– Erik2011-07-14 20:21:47 +00:00Commented Jul 14, 2011 at 20:21
Add a comment
|
2 Answers
If your intention is to allow blank fields, then use the original pattern of ^[a-zA-Z]{3}$ since the RegularExpressionValidator doesn't validate blank fields. It will allow them.
However, if you want to prevent blank entries then you'll need to add a RequiredFieldValidator to validate the same control, in addition to the RegularExpressionValidator.
5 Comments
agent-j
It sounds like the OP wants it to NOT be required. Do you think he would be ok with just the expression he has (
^[A-Za-Z]{3}$)? +1Piotr Perak
From what I understand OP wanted to allow empty field. RequiredFieldValidator won't allow empty field.
Ahmad Mageed
@agent-j if the field is optional and can be blank, then the
RegularExpressionValidator alone is sufficient since it will let blank entries through. To prevent blank entries the OP will need to use the RequiredFieldValidator in conjunction with the other validator.Ahmad Mageed
@agent-j, @Peri: updated to clarify. Thanks for the feedback!
Erik
Thank you, I want to allow a blank field so I will leave the initial regex and put the RequiredFieldValidator on the required fields in the form and leave it off the one that allows blanks, correct?