- RequiredFieldValidator Web Server Control
in ASP.NET environment,This article describes you how to work with RequiredFieldValidator Web Control.The RequiredFieldValidator can be used where without filling up an controls such as textbox.It restricts the user to specify the information in a desired textbox before leaving it.
Use this control to make an input control a required field. The input control fails validation if its value does not change from the InitialValue property upon losing focus. Multiple validators can be associated with the same input control. For example, a RequiredFieldValidator can be used to ensure input to a control, while at the same time a RangeValidator can be used to ensure that the input is within a specified data range.
To validate a required entry
- Go to Design View,Includes a RequiredFieldValidator control to the page and set the following properties:
- ControlToValidate -The ID of the control for which the user must provide a value.
- ErrorMessage, Text, Display -Properties that specify the text and location of the error or errors that will appear if the user skips the control.
- Add a test in your ASP.NET Web page code to check for validity
The following code snippets explains you the RequiredFieldValidator
-
<asp:Textbox id="txt1" runat="server"></asp:Textbox>
<asp:RequiredFieldValidator id="rfv1" runat="server"
ControlToValidate="txt1"
ErrorMessage="it is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>
-
- Formating the RequiredFieldValidator
ASP.NET validation controls allows you to customizing the format—font, size, and so on—of error text, or you can substitute a marker for error text. For example, you can have the validation control display an asterisk (*) when an error occurs.
You can also include a detailed error message in the ErrorMessage property of the validation control and add a ValidationSummary control to the page. The detailed ErrorMessage property text will appear on the page in the location of the ValidationSummary control.
To format error messages, specify the following properties,
- ForeColor - The color of the error message text.
- BackColor -The color behind the text.
- Font -The font face, size, weight, and so on.
- BorderWidth, BorderColor, and BorderStyle -The size and color of a border around the error message.
The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.
-
in .aspx.cs page,
-
void ValidateBtn_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
lblOutput.Text = "ok!";
}
else
{
lblOutput.Text = "not ok!";
}
}
-
in .aspx.cs page,
-
<table bgcolor="#eeeeee" cellpadding="10">
<tr valign="top">
<td colspan="3">
<asp:Label ID="lblOutput"
Text="Fill in the required field below"
runat="server"/>
<br>
</td>
</tr>
<tr>
<td colspan="3">
<b>Credit Card Information</b>
</td>
</tr>
<tr>
<td align="right">
Card Number:
</td>
<td>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
ErrorMessage="*"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_Click"
runat="server"/>
</td>
<td></td>
</tr>
</table>
-
- Related Links
-
d3cdd1c0-fa56-4b63-b926-cee0c8bdb5af|0|.0