Saturday 22 March 2014

Requiredfieldvalidator how to use

Required Field Validation – It Checks, does the control have any value. It’s used when you want the control should not be empty. It ensures that the user does not skip a mandatory entry field.  
Event will fire or control will validate only once user click on Button.
Important Property which need to handle for Required Field Validator is;


ControlToValidate – Linked with id which control to validate;
EnableClientScript - specifies client-side validation is enabled or not (true/false)
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



@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>
                    <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtValue"
                        ForeColor="Red" ErrorMessage="Enter Value !!"></asp:RequiredFieldValidator>
                </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" runat="server" ControlToValidate="txtPhoneNumber"
                        ForeColor="Red" ErrorMessage="Enter Phone Number !!"></asp:RequiredFieldValidator>
                </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" ControlToValidate="txtDOB"
                        ForeColor="Red" ErrorMessage="Enter date Of Birth !!"></asp:RequiredFieldValidator>
                </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