Saturday, 22 March 2014

how to use rangevalidator in asp.net

Range ValidatorCheck that the value in the validated control has specific defined range. user's input is in a given range(Number or Character).  
Event will fire or control will validate only once user click on Button.
Important Property which need to handle for Range Validator is;

ControlToValidate – Linked with id which control to validate;
EnableClientScript - specifies client-side validation is enabled or not (true/false)
DisplayContain three value None- the control is not displayed; Static - Control displays an error message if it’s not validate. Space is kept on the page for the message; Dynamic - Control displays an error message if it’s not validate. Space is not kept on the page for the message;
Enabled - specifies whether the validation control is enabled or not(true/false)
ErrorMessage - text to display in the ValidationSummary control when validation fails, it display in validation control also
ForeColorText color of message
Text - message to display when validation fails
ValidationGroup – Grouping all the validation control together
MaximumValue - Specifies the maximum value of the input control
MinimumValue - Specifies the minimum value of the input control
Type - Specifies the data type of the value to check. The types are:
·Currency
·Date
·Double
·Integer
·String


@Code Snippet – Html code
<body>
    <form id="form1" runat="server">
    <div>
        <table width="60%" align="center">
            <tr>
                <td colspan="2" align="left">
                    <h1>
                        Check validation</h1>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Name :
                </td>
                <td align="left">
                    <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
                  
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     
                </td>
            </tr>
            <tr>
                <td align="right">
                    Phone Number :
                </td>
                <td align="left">
                    <asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Display=Dynamic  runat="server" ControlToValidate="txtPhoneNumber"
                        ForeColor="Red" ErrorMessage="Enter Phone Number !!"></asp:RequiredFieldValidator>
                         <asp:RangeValidator ID="RangeValidator1" ControlToValidate="txtDOB" MinimumValue="2000000000"
                        MaximumValue="9999999999" Type="Double"  EnableClientScript="false" ForeColor="Red" Text="Phone number is not valid!!"
                        runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     
                </td>
            </tr>
            <tr>
                <td align="right">
                    Date Of Birth :
                </td>
                <td align="left">
                    <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"  Display=Dynamic  ControlToValidate="txtDOB"
                        ForeColor="Red" ErrorMessage="Enter date Of Birth !!"></asp:RequiredFieldValidator>
                    <asp:RangeValidator ID="rnvDOB" ControlToValidate="txtDOB" MinimumValue="1940-01-01"
                        MaximumValue="1996-12-31" Type="Date" EnableClientScript="false" ForeColor="Red" Text="You should be 18 year old!!"
                        runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Button" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
@OutPut screen
Once user not enter any value in text box and click on Button;

No comments:

Post a Comment

Others

You will like to read it