In asp.net we can use RequiredFieldValidator control and use the event handler of the control that completes the user's entry for the page to verify that validation was successful. for example, we need ensure that user has entered data in text box, such as first name and last name.
We must set two important properties when using the RequiredFieldValdiator control:
ControlToValidate The ID of the form field being validated
Text The error message displayed when validation fails
Code Example :
< %@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
< !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 >Code Example< /title >
< /head >
< body >
< form id="form1" runat="server" >
< div >
< asp:Label ID="Label1" runat="server" Text="First Name" Width="98px" >< /asp:Label >
< br / >
< asp:TextBox ID="txtFirstName" runat="server" >< / asp:TextBox >
< asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Fisrt Name Must Be Filled" ControlToValidate="txtFirstName" >< /asp:RequiredFieldValidator >< br / >
< br />
< asp:Label ID="Label2" runat="server" Text="Last Name" Width="98px" >< /asp:Label >
< br / >
< asp:TextBox ID="txtLastName" runat="server" >< /asp:TextBox >
< asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Last Name Must Be Filled" ControlToValidate="txtLastName">< /asp:RequiredFieldValidator>
< br />
< asp:Label ID="Label3" runat="server" Text="Age" Width="98px">< /asp:Label>
< br />
< asp:TextBox ID="TextBox3" runat="server" Width="35px">< /asp:TextBox>
< br />
< asp:Label ID="Label4" runat="server" Text="Adress" Width="98px">< /asp:Label>
< br />
< asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300px">< /asp:TextBox>
< br />
< asp:Button ID="Button1" runat="server" Text="Save" />< /div>
< /form>
< /body>
< /html>
No comments:
Post a Comment