Mandatory and regular expressions

I'm having issues with asp.net field validators taking up space on my page. I searched around and the documentation says use Display = "Dynamic" so that the validators do not take up space. However, when I use this, error messages are always displayed.

What am I doing wrong?

I just want the error messages to appear when the user either clicks the Save button or loses focus on the text box. And I do not want the validators to take their place.

<p>Please enter a new email:</p> <asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGgroup="Email" ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email" ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" /> <p>Please re-enter your email:</p> <asp:TextBox runat="server" ID="TextBoxEmail2" /> <asp:LinkButton ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" /> 
+4
source share
3 answers

The only thing I noticed in your code is a typo in ValidationGgroup="Email" . It must be ValidationGroup="Email"

Also, Display="Dynamic" works for me.

enter image description here

After clicking the submit button -

enter image description here

 <p> Please enter a new email:</p> <asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Email" ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server" Display="Dynamic"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email" ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" /> <p> Please re-enter your email:</p> <asp:TextBox runat="server" ID="TextBoxEmail2" /> <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TextBoxEmail" ControlToValidate="TextBoxEmail2" CssClass="failureNotification" Display="Dynamic" ErrorMessage="Must match." ValidationGroup="Email"></asp:CompareValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="Email" ErrorMessage="Please enter an confirm email" ControlToValidate="TextBoxEmail2" runat="server" Display="Dynamic"></asp:RequiredFieldValidator> <asp:LinkButton ID="LinkButton1" ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" /> 
+9
source

It worked out fine for me by deleting "ValidationGroup = ..". Havent looked at what it does, but it works fine without it and is not sure why it was needed (given that validator controls have an email text box control identifier).

+1
source

You can set the Display property to Dynamic or None, and the second to an error message that will only appear in the scan summary.

+1
source

All Articles