Required Field Validation Controls in Asp.Net C#

REQUIRED FIELD VALIDATION


The RequiredFieldValidator control ensures that the required field is not empty. It is generally tied to a text box to force input into the text box.

RequiredFieldValidator validation control make an asp.net web server input control as like a mandatory field.

                        DEMO

Html Coding 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr><td>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
 runat="server"
                ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
        </td></td></tr>
    <tr><td>
        <asp:Label ID="Label2" runat="server" Text="Country"></asp:Label>
        </td><td>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>Select Country</asp:ListItem>
                <asp:ListItem>Australia</asp:ListItem>
                <asp:ListItem>India</asp:ListItem>
                <asp:ListItem>UK</asp:ListItem>
                <asp:ListItem>US</asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
 runat="server"
                ControlToValidate="DropDownList1"
ErrorMessage="Please Select Country"
                InitialValue="Select Country"></asp:RequiredFieldValidator>
        </td></tr>
    <tr><td colspan="2">
        <asp:Button ID="Button1" runat="server" Text="Save" />
        </td></tr>
   
    </table>
    </div>
    </form>
</body>


</html>




You can use it to make sure that the user has entered something in a TextBox control



PropertyDescription
BackColorThe background color of the RequiredFieldValidator control
ControlToValidateThe id of the control to validate
DisplayThe display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
EnableClientScriptA Boolean value that specifies whether client-side validation is enabled or not
EnabledA Boolean value that specifies whether the validation control is enabled or not
ErrorMessageThe text to display in the ValidationSummary control when validation fails.Note: This text will also be displayed in the validation control if the Text property is not set
ForeColorThe foreground color of the control
idA unique id for the control
InitialValueSpecifies the starting value of the input control. Default value is ""
IsValidA Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
runatSpecifies that the control is a server control. Must be set to "server"
TextThe message to display when validation fails




First  - Add - New WebForm - Select  Textbox & DropDownlist  from ToolBox  







 
Next - Go to - ToolBox -  Validation -Select - Required Field Validation - for  Textbox & DropDownlist








 Next - Select Required Field Validation - F4 press Property window  below like that






Property Select - Error Message Change Required  - Select  Control to Validate TextBox id Values for Control Textbox 











Select - DropDownList -Required Field Validation - Error Message write required - Control to validate DropDownList ID - Initial values Put Select Country  or your dropdownlist values -  index 0 

















Next -  Press F5 - Run the Program 






Next  - Click Save Button  Without Fill  Values  Show below like that  Error Message Show 











Next -  Fill Values After Click - Save Button  - Save the values 








0 comments:

Post a Comment