Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required and RegularExpression Validators taking up space

Tags:

asp.net

I'm having issues with asp.net's field validators taking up space on my page. I've searched around, and documentation says to use Display="Dynamic" to keep the validators from taking up space. When I use this however, the error messages are always displayed.

What am I doing wrong?

I just want the error messages to display when the user either clicks the "Save" button, or loses focus on a textbox. And I don't want the validators to take up space.

     <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"  />
like image 565
JAck28 Avatar asked Dec 06 '22 08:12

JAck28


2 Answers

Only thing I notice in your code is you have a typo in ValidationGgroup="Email". It should be ValidationGroup="Email"

Other than that, Display="Dynamic" works for me.

enter image description here

After clicking, 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" />
like image 63
Win Avatar answered Feb 24 '23 15:02

Win


You can set the property Display to "Dynamic" or "none", the second one will cause the Error message to be displayed only in the validation summary.

like image 31
Pedro Emilio Borrego Rached Avatar answered Feb 24 '23 15:02

Pedro Emilio Borrego Rached