mobile ads

Wednesday 30 October 2013

ASP.NET Page Validation Controls


  • RequiredFieldValidator—Enables you to require a user to enter a value in a form field.
  • RegularExpressionValidator—Enables you to compare a value against a regular expression.
  • RangeValidator—Enables you to check whether a value falls between a certain minimum and maximum value.
  • CompareValidator—Enables you to compare a value against another value or perform a data type check.
  • CustomValidator—Enables you to perform custom validation.
  • ValidationSummary—Enables you to display a summary of all validation errors in a page.
You can associate the validation controls with any form controls included in ASP.NET Framework. For example, if you want to require a user to enter a value into a TextBox control, you can associate a RequiredFieldValidator control with the TextBox control.

<%@ Page Language="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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator 
                ID="RequiredFieldValidator1" runat="server"
                ErrorMessage="You must enter your name" 
                ControlToValidate="tbName"  ForeColor="Red"/>
        <br />
        Date of birth: <asp:TextBox ID="tbDOB" runat="server" />
        <asp:RegularExpressionValidator
                ID="RegularExpressionValidator1" runat="server"
                ErrorMessage="Enter a date in the form MM/DD/YYYY"
                ControlToValidate="tbDOB"  ForeColor="Red"
                ValidationExpression="(0[1-9]|1[012])/([1-9]|0[1-9]|[12][0-9]|3[01])/\d{4}"
        />
        <br />
        Amount of candies you eat daily (< than 1000): 
        <asp:TextBox ID="tbCandy" runat="server" />
        <asp:CompareValidator 
            ID="CompareValidator1" runat="server"
            ErrorMessage="Number is not < 1000"
            ControlToValidate="tbCandy"
            Operator="LessThan"  ForeColor="Red"
            ValueToCompare="1000" Type="Integer" />
        <br />
        Number of Doctor visits (1 - 10):
        <asp:TextBox ID="tbDr" runat="server" />
        <asp:RangeValidator 
                ID="RangeValidator1" runat="server"
                ErrorMessage="Enter a value from 1 to 10"
                ControlToValidate="tbDr"  ForeColor="Red"
                MinimumValue="1" MaximumValue="10" />
        <br />
        

        <asp:ValidationSummary runat="server" 
            ID="validationSummary"  ForeColor="Blue"/>

        <br />
        <asp:Button runat="server" Text="Submit" ID="btnSubmit" />
    </div>
    </form>
</body>
</html>


For detailed description in MSDN, Click here

0 comments:

Post a Comment