Cannot get asp: RegularExpressionValidator to work

I have a problem getting validation of the regular expression field for the asp page that I am trying to update.

Here is an asp: Panel divided into important bits:

<asp:Panel ID="pnlEmailAddressCollection" runat="server"> <div id="POMInput-wrapper"> <div class="POMInput-FieldText"> <span class="POMInput-wrapper-text">Name:</span> <br /> <span class="POMInput-wrapper-text">Email Address:</span> <br /> </div> <div class="POMInput-FieldEntry"> <asp:TextBox ID="txtEmailAddress" name="emailAddress" runat="server" CssClass="textInput"></asp:TextBox> <asp:TextBox ID="txtUserName" runat="server" name="firstName" CssClass="textInput"></asp:TextBox> </div> <asp:RequiredFieldValidator ID="rfvNameValidator" runat="server" ErrorMessage="Please enter your name" ControlToValidate="txtUserName" Display="None" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your email address" ControlToValidate="txtEmailAddress" Display="None" /> <asp:RegularExpressionValidator ID="rfvEmailValidator2" runat="server" ErrorMessage="Please enter a valid email address" ControlToValidate="txtEmailAddress" Display="None" ValidationExpression="^[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" EnableClientScript="true" /> </div> </asp:Panel> 

It does not currently work in any mailbox. Asp: RequiredFieldValidator works as expected.

I tested the regex in a test project, and the regex seems to be good (returns true in valid email messages, false on invalid). Did I install asp: RegularExpressionValidator incorrectly?

+4
source share
3 answers

You must remove the double backslash:

 ValidationExpression="^[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" 

Note that you put two backslashes where you meant only one. If you want to install this expression from the code behind, the line you specified is correct. But in aspx you don't need to hide the backslash.

Currently accepted email will be similar to abc @abc {backslash} .com

+1
source

You can try with this code

 ValidationExpression="[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" 

Note: you can remove the ^ and $ characters

0
source

The regular expression works in .NET (server-side), but is not executed due to client-side JScript implementation, as documented in the Notes . To verify this (it is being tested on the server side), set the EnableClientScript property on the validator to false.

Then undo this change and make sure that the regular expression will be passed on the client side. You can use the online tester if it’s easier for you.

0
source

Source: https://habr.com/ru/post/1441610/


All Articles