Validation
If you've
ever had to write client-side javascript to do form validation, you'll be happy
to learn that ASP.NET has several features that make validation a snap without
having to write a line of javascript. This article shows examples of the five
new validation controls in ASP.NET, and the 1 validation summary control.
-
RequiredFieldValidator
-
CompareValidator
-
RangeValidator
-
RegularExpressionValidator
-
CustomValidator
-
ValidationSummary
The validation controls are declared separately from the input controls,
allowing you to attach and detach multiple validation controls independently.
The controls are capable of determining the capabilities of the client, and for
up-level browsers, such as Internet Explorer, they are capable of producing
both client-side and server-side validation. For down-level browsers, such as
Netscape, server-side validation is used. It's important to note that even when
client-side validation is done, server-side validation is done as well. This
avoids the possibility of malicious users attempting to bypass the client-side
validation.
Validation controls can be configured to display error messages when their
validation fails. You can configure the control to dynamically insert the text,
meaning no HTML space is reserved for the message, or you can configure the
display as static, meaning that HTML space is reserved for the message. In both
cases, the message is hidden until the validation occurs.
Required Field Validation
The Required Field Validation is used to verify that a value is entered in the
target control. Validating that a Name has been input, for example, is
accomplished with the following:
<asp:RequiredFieldValidator id="valReqName"
runat="server" controlToValidate="txtName"
display="dynamic" errorMessage="Please enter in your
name."> </asp:RequiredFieldValidator>
Compare Validation
The Compare validator uses two fields, and compares their values. Values can be
compared for equality, inequality, greater than, less than, etc. For example,
in a form where a user is required to vote and confirm their vote, this code
verifies that the votes are the same.
<asp:CompareValidator id="compareVote2" runat="server"
controlToValidate="selVote2" controlToCompare="selVote"
type="String" operator="Equal" />
Range Validation
The Range Validation ensures that the value of a field is within a set of
values. For example, if you request an adult's age, you can verify that the age
is within a range of 18 and 120.
<asp:RangeValidator id="valRangeAge" runat="server"
controlToValidate="txtAge" type="Integer"
minimumValue="18" maximumValue="120" errorMessage="I'm
sorry, but you are not of voting age." display="dynamic" />
Regular Expression Validation
Regular Expression validation allows you to define a regular expression which
the field value must match. Visual Studio.NET has a number of built-in regular
expressions, including email address. The following code is used to verify that
the email address in the txtEmail field is of the form Name@Domain.com.
<asp:RegularExpressionValidator id="valRegEmail"
runat="server" controlToValidate="txtEmail"
display="dynamic"
validationExpression="^[\w-].*@[\w-]+\.(com|net|org|edu|mil)$"
errorMessage="Your email is not in a valid format." />
Custom Validation
The Customer validation control allows you to define your own validation
function. For example, your validation might be to verify a credit card, or to
check zip code against city and state, etc. You may have a custom javascript
library to perform client-side validation. The following snippet validates a
password field to be greater than 6 characters in length.
<SCRIPT LANGUAGE="JavaScript">
function
validateLength(oSrc,
args){
args.IsValid
=
(args.Value.length
>=
6);
}
</SCRIPT>
 
<asp:CustomValidator
id="CustomValidator1"
runat=server
  ControlToValidate
=
"txtPassword"
  ErrorMessage
=
"Passwords must be at least 6 characters."
ClientValidationFunction="validateLength"
>
</asp:CustomValidator>