By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

RegularExpression Validator?

P: n/a
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

Jul 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
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
Jul 28 '06 #2

P: n/a
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 wrote:
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

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
Jul 29 '06 #3

P: n/a
>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)

Arpan

Hans Kesting wrote:
>>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

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

Jul 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.