Friday, October 21, 2011
Validate using RequiredFieldValidator
In this tutorial you'll find out how to validate a control on a webform using RequiredFieldValidator. Using this you can show a warning and stop the submission of the form if a required field has no value.
Start a new ASP .NET Web Application project in Visual Studio .NET, named formValidation.
Add a new TextBox, a Button and RequiredFieldValidator:
The RequiredFieldValidator validates a field to see if a control has a value or not. In our example we use it to check if the box is empty or not.
We choose which control we need to check by changing the ControlToValidate property to the name of our TextBox control - TextBox1:
As you can see, you can easily do this in the Properties window of Visual Studio .NET. Also let's change the error message that will be displayed to the string 'This field is required'. We do this by changing the ErrorMessage property:
Try it. Don't enter anything in the TextBox and press the button, or just press anywhere on the page so that the TextBox loses focus. As expected, the message 'This field is required' is displayed in red, next to the TextBox.
Let's look into the HTML source of the webpage in Visual Studio .NET. In the form we can see how the properties (like theErrorMessage and ControlToValidate are set).
If you look into the webpage source after it is compiled, you'll see that the field is validated using client-side code written in JavaScript. This happens in Internet Explorer. But if you try the code in Mozilla Firefox, the HTML source is different - the validation is performed server-side, as not all browsers support JavaScript. And to be sure that the form is validated, if the client browser is not Internet Explorer (which supports JavaScript) server-side validation will be used.
So when you submit the form in Mozilla Firefox, if you don't enter any text in the TextBox and you press the button, you'll have to wait for the page to load again and the error to show up.
Maybe you want to set a default string for the TextBox like 'Type your name here', but if the user doesn't modify that value and submits the form? The property InitialValue of the RequiredFieldValidator helps you when you want not to validate the form when a specific value is entered - in our case when the 'Type your name here' string is in the TextBox, because that means that the user didn't change the value at all. And I don't believe there's someone who's name is actually 'Type your name here'.
To test this property, first change the Text property of TextBox1 to the string 'Type your name here'. Next change the InitialValueproperty of the RequiredFieldValidator to the same string. Run the code and see what happens. If you try to submit the form with the default value of the TextBox, it won't validate.
Perhaps you'll want to validate RadioButton controls, because in a list of RadioButton controls usually none of the controls are selected by default.
Add a RadioButtonList control to the page and add some items using the Items property (from the Properties window). Also add another RequiredFieldValidator. Set the ErrorMessage property to the string 'You must choose an option', and theControlToValidate property to RadioButtonList1:
If you don't choose one of the three options, the form will be invalid causing the 'You must choose an option' error to show up.
Start a new ASP .NET Web Application project in Visual Studio .NET, named formValidation.
Add a new TextBox, a Button and RequiredFieldValidator:
The RequiredFieldValidator validates a field to see if a control has a value or not. In our example we use it to check if the box is empty or not.
We choose which control we need to check by changing the ControlToValidate property to the name of our TextBox control - TextBox1:
As you can see, you can easily do this in the Properties window of Visual Studio .NET. Also let's change the error message that will be displayed to the string 'This field is required'. We do this by changing the ErrorMessage property:
Try it. Don't enter anything in the TextBox and press the button, or just press anywhere on the page so that the TextBox loses focus. As expected, the message 'This field is required' is displayed in red, next to the TextBox.
Let's look into the HTML source of the webpage in Visual Studio .NET. In the form we can see how the properties (like theErrorMessage and ControlToValidate are set).
<form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" Width="152px" Height="24px"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 24px" runat="server" Height="8px"ErrorMessage="This field is required" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 72px; POSITION: absolute; TOP: 48px" runat="server" Width="97px" Text="Button"></asp:Button> </form> |
If you look into the webpage source after it is compiled, you'll see that the field is validated using client-side code written in JavaScript. This happens in Internet Explorer. But if you try the code in Mozilla Firefox, the HTML source is different - the validation is performed server-side, as not all browsers support JavaScript. And to be sure that the form is validated, if the client browser is not Internet Explorer (which supports JavaScript) server-side validation will be used.
So when you submit the form in Mozilla Firefox, if you don't enter any text in the TextBox and you press the button, you'll have to wait for the page to load again and the error to show up.
The InitialValue property
Maybe you want to set a default string for the TextBox like 'Type your name here', but if the user doesn't modify that value and submits the form? The property InitialValue of the RequiredFieldValidator helps you when you want not to validate the form when a specific value is entered - in our case when the 'Type your name here' string is in the TextBox, because that means that the user didn't change the value at all. And I don't believe there's someone who's name is actually 'Type your name here'.
To test this property, first change the Text property of TextBox1 to the string 'Type your name here'. Next change the InitialValueproperty of the RequiredFieldValidator to the same string. Run the code and see what happens. If you try to submit the form with the default value of the TextBox, it won't validate.
Using RequiredFieldValidator to validate other controls
Perhaps you'll want to validate RadioButton controls, because in a list of RadioButton controls usually none of the controls are selected by default.
Add a RadioButtonList control to the page and add some items using the Items property (from the Properties window). Also add another RequiredFieldValidator. Set the ErrorMessage property to the string 'You must choose an option', and theControlToValidate property to RadioButtonList1:
If you don't choose one of the three options, the form will be invalid causing the 'You must choose an option' error to show up.