ASP.NET Validations Tips and Tricks Part-I

Sheonarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 5157 red flag
Rating: 5 out of 5  
 2 vote(s)

In this article we shall learn how to validate a TextBox as a mandatory field, how to validate a DropDownList / ListBox and What is Cause Validation property and how to avoid validation on clicking of a button.

Introduction

Validation controls are used to validate the ASP.NET input server controls like asp:TextBox for valid data when their data is submitted to the server. There are six validation controls in ASP.NET and they are

1. RequiredFieldValidator

2. RangeValidator

3. CompareValidator

4. RegularExpressionValidator

5. CustomValidator and

6. ValidationSummary

Get hundreds of ASP.NET, ADO.NET, jQuery, HTML, CSS and JavaScript How to Tips and Tricks, click here to get them.

Now, let's learn some Tips and Tricks related with Validation controls.

How to validate a TextBox as a mandatory field?

To make a TextBox mandatory for the end user to enter before a form can be submitted to the server, we can follow this approach.

ASPX PAGE

 

Username:<asp:TextBox ID="txtUserName" runat="server" />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ForeColor="Red"ControlToValidate="txtUserName" />

<p><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" /></p>

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{


}

In the above code snippet, we have a TextBox and a Button. As we want to validate the textbox as a mandatory field so we have used asp:RequiredFieldValidator with ErrorMessage that we want to display if the validation fails and ControlToValidate property set to the TextBox (in this case the textbox id is txtUserName) that should be validated.

OUTPUT


How to validate a DropDownList / ListBox?

To validate a DropDownList or ListBox as a mandatory field so that user has to select any item from them apart from pre-selected item, we can follow this approach.

ASPX PAGE

Select age: <asp:DropDownList ID="dropDownAge" runat="server">

<asp:ListItem Text="Select Age" Value="0" />

<asp:ListItem Text="18" Value="18" />

<asp:ListItem Text="19" Value="19" />

<asp:ListItem Text="20" Value="20" />

<asp:ListItem Text="> 20" Value=">20" />

</asp:DropDownList>

<asp:RequiredFieldValidator ID="reqAge" runat="server" ErrorMessage="Please

select age" ForeColor="Red" ControlToValidate="dropDownAge" InitialValue="0" />

<p><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" /></p>

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{


}

In the above code snippet, the first item of the DropDownList is “Select Age” with value as “0”; we want the user to select any age. To do that we can specify the RequiredFieldValidator's InitialValue as “0”, this will cause the RequiredFieldValidator fail unless user select any age other than “0” from the DropDownList.

OUTPUT

Note: Similarly we can perform ListBox validation too.


What is CauseValidation property and how to avoid validation on clicking of a button?

In case we have multiple buttons on the page and we want to avoid validation on clicking of certain button, we can use this approach.

ASPX PAGE

<p>

Username: <asp:TextBox ID="txtUserName" runat="server" />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !"

ForeColor="Red"ControlToValidate="txtUserName" />

</p>

<p>

Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />

<asp:RequiredFieldValidator ID="req2" runat="server" ErrorMessage="Mandatory !"

ForeColor="Red"ControlToValidate="txtPassword" />

</p>

<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="SubmitData" />

 

<asp:Button ID="Button1" runat="server" Text="Do something else"

CausesValidation="false"OnClick="DoSomethingElse" />

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{

}


protected void DoSomethingElse(object sender, EventArgs e)

{

}

 

In the above code snippet, on .aspx page we have Username and Password two textboxes. We have placed RequiredFieldValidator for both of them. Clicking “Login” button causes validation on both TextBoxes but clicking on “Do something else” button doesn't cause form validations because its CauseValidation property is set to false.

OUTPUT


Hope this article was useful. Thanks for reading !!!

Do let me know your feedback about the article and series of articles that has been published and going to publish, this encourages us to do better to help you understand clearly.

Subscribe to RSS feed to get the forthcoming articles alert directly into your inbox.

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)