Monday 5 March 2012

RequiredFieldValidator in Asp.Net

RequiredFieldValidator Control  
The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid. 

The RequiredFieldValidator control is used to make an input control a required field. With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string (""). Note: The InitialValue property does not set the default value for the input control. It indicates the value that you do not want the user to enter in the input control.
 
In the following example we declare two TextBox control, one Button control, and one Label control in an .aspx file. When the submit button is triggered, the submit subroutine is executed. The submit subroutine writes a text "You key already fully." to the Label control.





  1. how-to-use-RequiredFieldValidator-c.aspx (Design Page)
 
validation-controls
validation-controls



validation-controls
 

  2. how-to-use-RequiredFieldValidator-c.aspx (Source Page)
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="how-to-use-RequiredFieldValidator-c.aspx.cs" Inherits="how_to_use_RequiredFieldValidator_c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>How to Use RequireFieldValidator</title>
</head>
<body>
<form id="form1" runat="server">
    <div style="text-align: left">
        First Name: <asp:TextBox ID="txtFirstname" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="txtFirstname" ErrorMessage="You no feed the Name !"></asp:RequiredFieldValidator>
        <br />
        Last Name:<asp:TextBox ID="txtLastname" runat="server" Wrap="False"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ControlToValidate="txtLastname" ErrorMessage="You no feed the Last Name !"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Button ID="BtnSubmit" runat="server" Text="Submit"
            onclick="BtnSubmit_Click" />
        <br />
        <br />
        <asp:Label ID="lblSucceed" runat="server"></asp:Label>
    </div>
</form>
</body>
</html>
3. how-to-use-RequiredFieldValidator-c.aspx.cs (Code Behind C# Language)using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class how_to_use_RequiredFieldValidator_c : System.Web.UI.Page
{
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        lblSucceed.Text = "You key already fully.";
    }
}
 
  4. how-to-use-RequiredFieldValidator-c.aspx (View in Browser)
 
validation-controls
  4. how-to-use-RequiredFieldValidator-c.aspx (View in Browser)
 
validation-controls
 
validation-controls
 
          

No comments :