required field validator in asp.net

Now I want to write required field validator in asp.net, RequiredFieldValidator for drop down list in asp.net, required field validator for textbox in asp.net.

RequireFieldValidator control is most used validation control in the family of validation controls. Despite its broad use it still very easy to implement it.We just need to take care some basic things and also some basic properties. In most of the aspx pages the user interface is heavily depends on textbox and dropdown. So for both of this control we can use RequireFieldValidator. I will first go through the basic properties for it and then move on to the real practical scenario.

Property Use of Property
ErrorMessag set the error message to display user.
ControlToValidate The id of the control for which you want to use this validation control. It can be a textbox or DropDown control id as per your need.
InitialValue This property will contain the initial value of your control.
ValidationGroup Name of the validation group to which this validation control belongs.
Display Used to set the display behavior of the error message in a validation control.We can set static or Dynamic. If we set it as a static then the errormessage will display whether it is valid or not. If we set it as a dynamic the erromessage would only come up once the validation get fired and it is not valid.
CssClass Used to set the css for validation control.
SetFocusOnError Used to set a value that indicates whether focus is set to the control specified by the ControlToValidate property when validation fails.
Enabled Used to set a value that indicates whether the validation control is enabled.(true/false)

Now lets move on to the practical scenario for this. Suppose i want to validate a textbox which needs some input from end user before proceed further.Here is my code for the same.

<table>
            <tr>
                <td>First Name :</td>
                <td>
                    <asp:TextBox ID="txtFirstName" runat="server">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqFirstName" runat="server" ControlToValidate="txtFirstName" 
                        CssClass="errMessage" Display="Dynamic" Enabled="true" ErrorMessage="Please enter first name." 
                        ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSave" runat="server" ValidationGroup="vgSave" />
                </td>
            </tr>
        </table>
 Require field validator for Textbox
Require field validator for Textbox

Now suppose i want to validate my dropdown using RequireFieldValidator. I have a gender DropDown with three options(Select,Male,Female). I want to force user to select male or female from the dropdown list. “select” is not allowed. Here is my code for the same.

<table>
            <tr>
                <td>Gender :</td>
                <td>
                    <asp:DropDownList ID="ddlGender" runat="server">
                        <asp:ListItem Value="0" Text="Select"></asp:ListItem>
                        <asp:ListItem Value="1" Text="Male"></asp:ListItem>
                        <asp:ListItem Value="2" Text="Female"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="reqGender" runat="server" ControlToValidate="ddlGender" InitialValue="0"
                        CssClass="errMessage" Display="Dynamic" Enabled="true" ErrorMessage="Please select gender."
                        ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSave" runat="server" ValidationGroup="vgSave" Text="Save" />
                </td>
            </tr>
        </table>
Require field validator for dropdown
Require field validator for dropdown

its been a good habit to check that page is valid on button click event. Use below code to check whether the page is valid or not.

protected void btnSave_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        //Your Code to save page values
    }
}

Now use both of the above example in single page and you can see the real impect of validationGroup property. It allows us to divide our validation controls in a group where it will get fired based on the associated button event for which you have defined validationGroup.

Leave a Reply