2

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}$

2
  • 1
    Where a blank field means any amount of white space, or nothing entered at all? Commented Jul 14, 2011 at 19:15
  • 1
    Sorry 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 field Commented Jul 14, 2011 at 20:21

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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}$)? +1
From what I understand OP wanted to allow empty field. RequiredFieldValidator won't allow empty field.
@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.
@agent-j, @Peri: updated to clarify. Thanks for the feedback!
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?
0

Have you tried using (^$)|(^[a-zA-Z]{3}$)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.