Validating User Input in ASP.NET Part III

Posted on October 4, 2007 - Filed Under ASP.NET |

Using Validation Controls

RequiredFieldValidator 

The first thing that we will need to do is insure that the user has at least attempted to complete certain fields. In our example, the only required field is the Full Name field. In order to validate that a user enters information in the Full Name field, we simply insert a RequiredFieldValidator control next to the field we want to validate as shown in this example:

<%@ Page Language=”VB” %>

<script runat=”server”>

Sub Page_Load(Source as Object, e as EventArgs)

      If Not IsPostBack then

              lblTitle.Text = “Leave the field blank and submit” 

     else

              lblTitle.Text = “Submit was successful”

     end if

end sub 

</script>

<html>

<head>

     <title>Using RequiredFieldValidator Control in ASP.NET</title>

</head>

 <body>

     <form ID=”Webform1″ Method=”post” Runat=”Server” Name=”Webform1″>

           <p><asp:Label ID=”lblTitle” Runat=”server” /></p>

           <p>

           Full Name<asp:TextBox ID=”txtName” Runat=”server”></asp:TextBox>

           </p>

           <p>

           <asp:RequiredFieldValidator ID=”valReqName” ErrorMessage=”You must fill in the full name field” Runat=”server” ControlToValidate=”txtName” Display=”Static”>

         </asp:RequiredFieldValidator>

         </p>

         <p>

        <asp:Button ID=”btnSubmit” Runat=”server” Text=”Submit”></asp:Button>

         </p>

         </form>

</body>

</html>

to be continued….

Comments

Leave a Reply