Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Wednesday 13 March 2013

Required Field Validator Control In ASP.NET

Starting from this tutorial I am going to write a series of tutorials to cover asp.net validation controls. Seven validation controls are available to you in ASP.NET 4 and no new validation server control have been added in asp.net since the initial release.

The available validation server controls are:

  1. RequiredField Validator
  2. Compare Validator
  3. Range Validator
  4. RegularExpression Validator
  5. Custom Validator
  6. Dynamic Validator
  7. ValidationSummary

Let's have a look at the first control.

1. Required Field Validator:-

It simply checks whether something entered into the HTML form element and ensures that the user does not skip an entry field on form. It is simple validation control that is why it is most frequently used.

Example:-

Consider a simple example in which only one text box,a submit button,label and a Required Field Validator control is used. Do the following steps or copy and paste the below code into your web form.

  1. Open your web form.
  2. Drag and drop a text box and name it as usertxtbox.
  3. Drag and drop a RequireFieldValidator control from Toolbox.
  4. Drag and drop one label and one button.

RequireFieldValidator.aspx:



  <html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
        <title>Required Field Validator</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <label>
        User Name:
        </label>
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
       runat="server" ControlToValidate="txtUserName" 
       ErrorMessage="Please enter user name." 
       ForeColor="Red"></asp:RequiredFieldValidator>
	        <br /><br />
	        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
	            onclick="btnSubmit_Click" />
	        <asp:Label ID="Label1" runat="server" ></asp:Label> 
	    </div>
	    </form>
	</body>
	</html>
    

RequireFieldValidator.aspx.cs:



  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
	
namespace DynamicWebLearning
{
    public partial class RequireFieldValidator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid) {
                Label1.Text = "Page is valid!!!!!";
            }
        }
    }
}

Run this page and you will see a text box and a submit button. Don't enter any value inside text box and click the submit button and you will see the result as shown in the figure below.


The first property to look at is the ErrorMessage property. This property is the value that is shown to the end user via the Web page if the validation fails. In this case, it is a simple "Please enter user name." string. The second property is the ControlToValidate property. This property is used to make an association/connection between validation server control and the ASP.NET form element that requires the validation. That is why the id of the text box and ControlToValidate property have same value ("txtUserName").

Hope it will be helpful for you. Your opinion is valuable for us so do write comments and share your opinion.

No comments:

Post a Comment

Please do not enter spam links.