RegularExpression Validator?

A

Arpan

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
 
H

Hans Kesting

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
 
A

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

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 said:
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
 
H

Hans Kesting

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top