With the release of ASP.Net 2.0 came a host of Validation features. In this series I'll be touching on those and show you how easily it now is to create validation for your forms.
In part 1 i'll be showing a new feature/property of existing validation tools – namely the ValidationGroup property. With this property you can now gather section specific validation together, so now there's no need to assess what to validate on PostBack.
In forms where you have several sections – some which might be dependant on others – some which might be individual and not be required. You can still validate these very easily should somebody input data into the fields.
For the purpose of illustrating this feature i'll be creating a very simple login form (not using any of the Login controls in ASP.Net 2.0).
as you can see, i've used the following controls here:
- Label – Username
- Label – Password
- TextBox – Username
- TextBox – Password
- Button – Login
- RequiredFieldValidator – Username
- RequiredFieldValidator – Password
To group them all together in the same ValidationGroup you need to set the group name in the ValidationGroup property, which can be found at the very bottom of the property list if you sort it alphabetically.
I chose to use LoginForm as the ValidationGroup name. This will have to be done for each control you want to be grouped together.
Now on each of the RequiredFieldValidator controls i'll set the ControlToValidate and Display property.
Now the reason for selecting None in the Display property is that i want the ErrorMessage value to display in the ValidationSummary.
Simple as that – you've now grouped the fields together with the ValidationSummary and both RequiredFieldValidator controls and can have several grouped validation rules in place on the same form.