I’ve been reading an ASP.Net book lately and just got to the information about Validation controls. These seem like a pretty sweet way to get a lot done with very little code. For example, say a that I want a text field that is required, all I have to do is add:
The RequiredFieldValidator also has an initial value as a property, and will fail if the field is still that initial value. This is especially useful with Drop Downs that you want to make sure that the user has selected a value in:
Now if the user does not change the value of the Drop Down, the validation will fail.
You can use one CompareValidator to validate the value and type of a field. Setting the Operator to something besides “DataTypeCheck” and setting the Type property will both make sure the value fulfills the comparison and is of the right type. For example, if you wanted the user to input an integer greater than 0, you could use the following validation control:
Another thing to note is that by default these validation controls will take up the space on the page required by the value of the Text property. Setting the Display property to “Dynamic” will make them only take up space if the message is visible. If you plan on having multiple validations on the same control, you should make the Display=”Dynamic” so that you don’t end up with strange looking blank spots.
Here’s the plug: I’ve been reading “Core Internet Application Development with ASP.Net 2.0″ by Randy Connolly. I’ve found it quite good so far.