0

I have the following regular expressions, used to pick up a specific domain name and email address type:

(?i:^domain\\[a-z0-9]+$)
(?i:([;]|^)([a-z0-9.]+@(dom\.net|part1\.part2\.uk))(?=[;]|$))

I'm using them in a pair of RegularExpressionValidator controls on an ASP.NET page to do the obvious.

The two expressions have been tested (first, second), and work fine. I've even tested them on regexstorm.net/tester, and they work fine there, too, or rather, it didn't complain at all.

I've tried entering data correctly and incorrectly into the text boxes, which does produce the desired result (i.e. showing and hiding the validator messages).

When I come to submit the changes for the data to be saved away, though, nothing happens.

This thread in the ASP.NET forums showed me that the issue could be related to an incorrectly defined expression. After testing and pulling the results from the developer tools with Chrome, sure enough I did find an exception listed, quoting an incorrect group expression (e.g:):

Uncaught SyntaxError: Invalid regular expression: 
    /(?i:([;]|^)([a-z0-9.]+@(dom\.net|part1\.part2\.uk))(?=[;]|$))/: 
Invalid group
at new RegExp (<anonymous>)
at HTMLSpanElement.RegularExpressionValidatorEvaluateIsValid [as evaluationfunction] (ScriptResource.axd?d=nv7asgRUU0tRmHNR2D6t1I81CZYvhDRBNs2fZ336VJmjaWUtfpNHyosZQUxZ0B5WPXmV97rd4uGrASTweyiB-WOW1uyNsuPtZ3w36cKlE-LnO2_rZ0xo0PpdoCQY_z4Id3GA-_iewMRfjN0CNhHZOQ2&t=26028d1e:458)
at ValidatorValidate (ScriptResource.axd?d=nv7asgRUU0tRmHNR2D6t1I81CZYvhDRBNs2fZ336VJmjaWUtfpNHyosZQUxZ0B5WPXmV97rd4uGrASTweyiB-WOW1uyNsuPtZ3w36cKlE-LnO2_rZ0xo0PpdoCQY_z4Id3GA-_iewMRfjN0CNhHZOQ2&t=26028d1e:200)
at ValidatorOnChange (ScriptResource.axd?d=nv7asgRUU0tRmHNR2D6t1I81CZYvhDRBNs2fZ336VJmjaWUtfpNHyosZQUxZ0B5WPXmV97rd4uGrASTweyiB-WOW1uyNsuPtZ3w36cKlE-LnO2_rZ0xo0PpdoCQY_z4Id3GA-_iewMRfjN0CNhHZOQ2&t=26028d1e:162)
at HTMLInputElement.eval (eval at ValidatorHookupEvent (ScriptResource.axd?d=nv7asgRUU0tRmHNR2D6t1I81CZYvhDRBNs2fZ336VJmjaWUtfpNHyosZQUxZ0B5WPXmV97rd4uGrASTweyiB-WOW1uyNsuPtZ3w36cKlE-LnO2_rZ0xo0PpdoCQY_z4Id3GA-_iewMRfjN0CNhHZOQ2&t=26028d1e:90), <anonymous>:3:1)

Unfortunately, I simply don't know enough about regex to be able to figure out just where the expression is breaking down - I would really appreciate some pointers.

The ASP.NET page code for the related text boxes and validators...

<asp:Label runat="server" ID="labelDomainUsername" 
    Text="Your domain username..." AssociatedControlID="textDomainUsername" 
    ClientIDMode="Predictable" />
<span class="help">
    This is your Windows login, and usually appears as <strong>DOMAIN\asurname</strong>.
</span>
<asp:TextBox runat="server" ID="textDomainUsername" 
    Text='<%# Bind("domain_username")%>' 
    CssClass="mandatory w2Eighths noBlock" 
    PlaceHolder="DOMAIN\AUsername" TabIndex="0" />
<asp:RegularExpressionValidator runat="server" ID="rexDomainUser"
    ControlToValidate="textDomainUsername" 
    ValidationExpression='<%$ appSettings:rexDomainAccount %>'
    Text="Please supply a valid domain username."
    CssClass="validBalloon" Display="Dynamic" />

<asp:Label runat="server" ID="labelEmail" 
    Text="Your email address..." AssociatedControlID="textEmail" />
<span class="help">
    Your email address can be an <strong>dom.net</strong> or <strong>part1.part2.uk</strong>
    account. If you have a secretary, or wish to include another email address separate them with a 
    semi-colon.
</span>
<asp:TextBox runat="server" ID="textEmail" Text='<%# Bind("email")%>' 
    CssClass="mandatory w3Eighths noBlock" AutoCompleteType="Email" 
    PlaceHolder="any.username@dom.net" TabIndex="1" />
<asp:RegularExpressionValidator runat="server" ID="rexEmail"
    ControlToValidate="textEmail" 
    ValidationExpression='<%$ appSettings:rexEmailAddress %>'
    Text="Please supply a valid email address."
    CssClass="validBalloon" Display="Dynamic" />
Paul
  • 4,160
  • 3
  • 30
  • 56
  • You have backlashes in your regex. Are you escaping them or surrounding your string using `@` such as `@"regexHere"` instead of `"regexHere"` – ctwheels Nov 23 '17 at 16:33
  • The backslashes are escaped. The single slashes are escaping the dot characters (`\.`), and the double slashes are escaping the slash `\\\`. – Paul Nov 23 '17 at 16:37
  • Can you post the code that runs the regex? – ctwheels Nov 23 '17 at 16:39
  • That's the ASP.NET stuff (there is no code behind). – Paul Nov 23 '17 at 16:52
  • Try using `/:([;]|^)([a-z0-9.]+@(dom\.net|part1\.part2\.uk))(?=[;]|$)/i` instead. [This](https://stackoverflow.com/questions/20325114/asp-net-validationexpression-to-ignore-case-sensitivity) shows a common issue with RegularExpressionValidator, although I'm not convinced it's your issue. – ctwheels Nov 23 '17 at 16:56
  • Thanks @ctwheels. I'll give it a go and let you know. – Paul Nov 23 '17 at 17:14
  • @ctwheels: Thanks for your help with this one. – Paul Nov 27 '17 at 10:29

1 Answers1

0

As pointed out, there are certain issues with utilising the RegularExpressionValidator. There are multiple ways around the issue, but all I've done is replace it with a CustomValidator and validate the parts of the string that I needed too.

Paul
  • 4,160
  • 3
  • 30
  • 56
  • Ha! Ha! I was asked whether I was a bot or a human on posting this! They must be messing with our network again. ;o) – Paul Nov 27 '17 at 10:28