Banner Ad

Thursday, January 10, 2013

Validators in ASP.Net – Part 2

By Francis   Posted at   12:36 PM   Validators in ASP.Net
Range Validator:
In my Previous post, I have explain about the Required Field Validator. In this article I’m going to explain about the Range Validator.Like Required Field Validator, Range validator is a server side control, which is used to validate a value falls between 2 values.
Add the below code in the .aspx page.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ValidatorDemo.aspx.vb"
    Inherits="AllInOne.ValidatorDemo" %>

<!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>
        <table>
           
            <tr>
                <td>
                    Enter Your Age:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
                </td>
                <td>
                    <asp:RangeValidator ID="intRangeValidator" runat="server" ControlToValidate="txtAge" MinimumValue="1"
                        MaximumValue="100" Display="Static" Type="Integer" EnableClientScript="false"
                        ErrorMessage="Age must be between 1 to 100">
            
                    </asp:RangeValidator>
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit Age" />
                </td>
            </tr>
            <tr>
                <td>
                    Enter Your DOB:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtDOB"></asp:TextBox>
                </td>
                <td>
                    <asp:RangeValidator ID="datRangeValidator" runat="server" ControlToValidate="txtDOB" 
                        MinimumValue="1990-01-01" MaximumValue="2002-5-31" Display="Static" Type="Date" EnableClientScript="false"
                        ErrorMessage="DOB must be between 1/1/1990 and 31/5/2013!">
            
                    </asp:RangeValidator>
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Submit DOB" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 
 
In the above example, we have added 2 text boxes and 2 range validator controls with Different “Type” attribute. One is for Integer and another one is for Date.
 
Range Validator  also provides the same properties like the Required Field validator as well as few more additional property which is exclusively owned by this validator.


Attribute
Description
runat
This is the server control, which is processed by the WEB Server (IIS). So this is the required attribute for every server control.
Control ToValidate
This attribute is used to denote which control we need to validate.  In this example, we give the ID of the name text box “txtAge”.
ErrorMessage
Used to give the Error message, when validation fails.
MinimumValue
Indicates the Minimum value to Compare.
MaximumValue
Indicates the Maximum value to Compare.
Type
The compared values’ data type.  The data type can be any one of the following:  String, Date, Integer, Currency and Double.
Display
Denotes the display behavior of this control.
        None – The control is not displayed. Instead of that “ValidationSummary” control has been used to display error.
       Static – The control displays the Error message  if validation failed. The error message space has been reserved.
          Dynamic – Same as static, but when this has been set error message space not reserved.
EnableClientScript
Used to denote enable/disable the client script

Happy Programming!!!


About Francis

Francis, an Associate at Cognizant. Having 7+ Years of experience in Microsoft web technologies.

Connect with Us