RegularExpression Validator?

  • Thread starter Thread starter Arpan
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top