Reputation: 13
I am trying to validate a string entered into a textbox. I want to make sure that the first 2 characters are either 02, 04 or 09.
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "check_number" ID="rxvValidCheckNumber1" ValidationExpression = "^(02|04|09)" runat="server" ErrorMessage="Valid Check Number required."></asp:RegularExpressionValidator>
If I enter a string that begins with 02, 04 or 09 the ErrorMessage still fires. What am I doing wrong?
Upvotes: 1
Views: 761
Reputation: 626802
The ValidationExpression
regex is anchored by default, and thus you need to match the entire input. You may match 0+ chars with .*
:
ValidationExpression = "^(02|04|09).*"
To make it a bit more "elegant", you may use 0[249]
after ^
:
ValidationExpression = "^0[249].*"
The expression matches
^
- start of string anchor0
- a 0
digit[249]
- a character class matching either 2
or 4
or9
.*
- any 0+ chars other than line break chars.If your textobx is multiline, you need to use (?s)
singleline/dotall modifier
ValidationExpression = "(?s)^0[249].*"
or (to enable client side validation, the (?s)
is not supported in JavaScript):
ValidationExpression = "^0[249][\s\S]*"
where [\s\S]
matches any char including a line break char.
Upvotes: 2