Search code examples
asp.netregexvalidationformview

FormView not saving due to RegularExpressionValidator grouping issue in expression


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="[email protected]" TabIndex="1" />
<asp:RegularExpressionValidator runat="server" ID="rexEmail"
    ControlToValidate="textEmail" 
    ValidationExpression='<%$ appSettings:rexEmailAddress %>'
    Text="Please supply a valid email address."
    CssClass="validBalloon" Display="Dynamic" />

Solution

  • 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.