HI WELCOME TO SIRIS

Do proper alignment of validator control's error messages using Display property


In Asp.net, sometimes we need to validate a TextBox and its input by more than one validator controls. Suppose you Want to validate a TextBox that must contain a valid Email address then we required two validator controls - Required Field and Regular Expression as shown below:

Validator without Display Property (By default Display is Static)

When we doesn’t set the display property of both the below validator controls then error message of second validator control will display on the page just after the space that is taken by first validator error message as shown below :
  1. <form id="form1" runat="server">
  2. <div>
  3. Enter Email ID : <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  4. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtEmail" runat="server" ForeColor="Red" ErrorMessage="Please Enter Email ID"></asp:RequiredFieldValidator>
  5. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Please Enter Correct Email ID"></asp:RegularExpressionValidator>
  6. </div>
  7. <br />
  8. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  9. </form>


Validator with Display Property

When we set the display property of both the below validator controls then error message of second validator control will display on the page just after the TextBox as shown below :
  1. <form id="form1" runat="server">
  2. <div>
  3. Enter Email ID : <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  4. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtEmail" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please Enter
  5. Email ID"></asp:RequiredFieldValidator>
  6. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" Display="Dynamic" ForeColor="Red" ErrorMessage="Please
  7. Enter Correct Email ID"></asp:RegularExpressionValidator>
  8. </div>
  9. <br />
  10. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  11. </form>

Summary
In this article I try to explain how can you align the error messages of validator controls properly using Display property. I hope after reading this article you will be able to do proper alignment of your validator control's error messages. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.