Reputation: 1959
I've created an email validator for ASP.NET with a 100 Character limit. It all worked fine and then I did a refresh like 20 minutes later and I'm getting the following error.
parsing "[\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)]{0,100}" - [x-y] range in reverse order. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: parsing "[\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)]{0,100}" - [x-y] range in reverse order.
So my code is <asp:RegularExpressionValidator ID="emailReg" runat="server" ControlToValidate="email" ValidationExpression="[\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*]{0,100}" Text=" <img src='err.png' alt='*' title='Please enter a valid email address less than 100 characters long'>" ErrorMessage="Please enter a valid email address less than 100 characters long"></asp:RegularExpressionValidator>
Can any one tell me what's suddenly gone wrong? I haven't changed anything.
Upvotes: 0
Views: 626
Reputation: 56915
The regex probably wouldn't have worked in the first place - it's probably not valid, and certainly not doing what you expect it to.
By including your entire regex in the character class [ ]{1,100}
, everything within that character class is not interpreted as a regex, but instead as contents of a character class.
That is, your regex matches 0 to 100 of \w
,+
,*
,(
,... etc.
The error comes from the hyphens within: if I do a regex [a-z]
, it means "match all characters between a and z (inclusive)".
Your regex has [-+
and [-.
, all within the []{0,100}
. So, you're trying to say "match all characters between [
and +
". This is defined by the unicode character. The left square bracket [
has unicode 91, whilst +
is unicode 43.
The error message you are getting is because you're trying to select all characters between unicode points 91 and 43, which is back to front.
Anyhow, to fix your regex, take it out of the []
.
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
To enforce the 100 character limit in regex, you'll have to use a lookahead:
^(?=.{0,100}$)\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
The ^(?=.{0,100}$)
makes sure there are at most 100 characters in the string. If this is true, the rest of the regex then proceeds to validate it.
Upvotes: 1