Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RegularExpression Validator?

0 views
Skip to first unread message

Arpan

unread,
Jul 28, 2006, 1:48:42 AM7/28/06
to
Consider the following code which validates a TextBox using
RegularExpression:

<form id="form1" runat="server">
<asp:TextBox id="txt1" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="txt1"
ValidationExpression="[0-9]{5}-[0-9]{4}|[0-9]{5}" ErrorMessage
="Invalid Input" Display="Dynamic" runat="server"/>
<asp:Button id="btn" Text="SUBMIT" runat="server"/>
</form>

The above RegularExpression Validator control doesn't display the error
message if the text entered in the TextBox is, say, 90210. Why?

Moreover, if the text entered in the TextBox is, say, 54963-87521, then
the error message gets displayed. Why?

I want the text input to have any 5 numbers between 0 & 9 which is
validated by [0-9]{5} followed by a dash which is validated by '-' &
finally followed by either any four numbers between 0 & 9 'or' any five
numbers between 0 & 9 which is validated by [0-9]{4}|[0-9]{5}. So how
is the Validator control passing 90210 but not passing 54963-87521?

Thanks,

Arpan

Hans Kesting

unread,
Jul 28, 2006, 5:16:53 AM7/28/06
to

It's validating as "5 digits, dash, 4 digits" or "just 5 digits".
You need to use () to give the | the required scope:
[0-9]{5}-([0-9]{4}|[0-9]{5})

But even better would be:
[0-9]{5}-[0-9]{4,5}

to accept "between 4 and 5" digits for the second group.

Hans Kesting


Arpan

unread,
Jul 29, 2006, 2:33:22 PM7/29/06
to
> It's validating as "5 digits, dash, 4 digits" or "just 5 digits".
> You need to use () to give the | the required scope:
> [0-9]{5}-([0-9]{4}|[0-9]{5})

PLEASE DO NOT MODIFY THE REGULAREXPRESSION I CITED IN POST #1. I am not
asking for alternatives to that RegularExpression. All I want to know
is HOW does the value 90210 pass the RegularExpression
[0-9]{5}-[0-9]{4}|[0-9]{5}?

The RegularExpression validates that the value in the TextBox should
have any 5 numbers between 0 & 9 followed by a dash & finally followed
by either 4 numbers between 0 & 9 or 5 numbers between 0 to 9. This
means that after the 1st 5 numbers, there MUST BE a dash. After the
dash, there MUST BE either 4 or 5 numbers; so how does 90210 pass the
test? Where is the dash in 90210? So shouldn't 90210 fail to pass the
test then & there itself?

Arpan

Hans Kesting

unread,
Jul 31, 2006, 3:20:06 AM7/31/06
to
>> It's validating as "5 digits, dash, 4 digits" or "just 5 digits".
>> You need to use () to give the | the required scope:
>> [0-9]{5}-([0-9]{4}|[0-9]{5})
>
> PLEASE DO NOT MODIFY THE REGULAREXPRESSION I CITED IN POST #1. I am not
> asking for alternatives to that RegularExpression. All I want to know
> is HOW does the value 90210 pass the RegularExpression
> [0-9]{5}-[0-9]{4}|[0-9]{5}?
>
> The RegularExpression validates that the value in the TextBox should
> have any 5 numbers between 0 & 9 followed by a dash & finally followed
> by either 4 numbers between 0 & 9 or 5 numbers between 0 to 9. This
> means that after the 1st 5 numbers, there MUST BE a dash. After the
> dash, there MUST BE either 4 or 5 numbers; so how does 90210 pass the
> test? Where is the dash in 90210? So shouldn't 90210 fail to pass the
> test then & there itself?
>

READ THE REST OF MY POST!

(and don't shout)

0 new messages