0% found this document useful (0 votes)
57 views

Using The Validation Controls

Validation controls in ASP.NET help ensure users enter correct information into forms. There are six main validator controls - RequiredField, Range, Compare, RegularExpression, Custom, and ValidationSummary. Validators can perform client-side or server-side validation. Properties like ControlToValidate, ErrorMessage and IsValid determine which field is validated and the error messages. Regular expressions and custom validators provide flexibility to validate complex formats like emails and phone numbers. Manual validation is also possible by disabling client-side validation or adding a non-validating submit button.

Uploaded by

Jaspreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Using The Validation Controls

Validation controls in ASP.NET help ensure users enter correct information into forms. There are six main validator controls - RequiredField, Range, Compare, RegularExpression, Custom, and ValidationSummary. Validators can perform client-side or server-side validation. Properties like ControlToValidate, ErrorMessage and IsValid determine which field is validated and the error messages. Regular expressions and custom validators provide flexibility to validate complex formats like emails and phone numbers. Manual validation is also possible by disabling client-side validation or adding a non-validating submit button.

Uploaded by

Jaspreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Using The Validation Controls

Understanding Validation
Users might ignore an important field and leave it
blank.
Users might try to type a short string of nonsense to
circumvent a required field check, thereby creating
endless headaches on your end. E.g. Emails.
Users might make an honest mistake, such as
entering a typing error, entering a nonnumeric
character in a number field, or submitting the wrong
type of information.

Overview Of Validator Controls

Six Validation Controls are included in ASP.NET


3.5 Framework:
RequiredFieldValidator
RangeValidatior
CompareValidator
RegularExpressionValidator
CustomValidator
ValidationSummary

CausesValidation Property
when the user clicks the button depends on the value of
the CausesValidation property:
If CausesValidation is false, ASP.NET will ignore
the validation controls, the page will be posted back,
and your event-handling code will run normally.
If CausesValidation is true (the default), ASP.NET
will automatically validate the page when the user
clicks the button.If any control fails to validate, ASP.NET
will return the page with some error information,
depending on your settings.

Client Side Validation - In this case, when the


user clicks a CausesValidation button, the
same error messages will appear without the
page needing to be submitted and returned
from the server. This increases the
responsiveness of your web page.

The Validation Controls


Validation Controls are found in
System.Web.UI.Web.Controls namespaces.
Inherits From Base Validator class.

Properties Of BaseValidator Class


ControlToValidate - Identifies the control that this validator
will check. Each validator can verify the value in one input
control.
ErrorMessage and ForeColor - If validation fails, the
validator control can display a text message (set by the
ErrorMessage property). By changing the ForeColor, you
can make this message stand out in angry red lettering.
Display - Allows you to configure whether this error
message will be inserted into the page dynamically when
its needed (Dynamic) or whether an appropriate space will
be reserved for the message (Static). you can also choose
None to hide the error message altogether.

Contd
IsValid - After validation is performed, this
returns true or false depending on whether it
succeeded or failed.
Enabled - When set to false, automatic validation
will not be performed for this control when the
page is submitted.
EnableClientScript - If set to true, ASP.NET will
add JavaScript and DHTML code to allow clientside validation on browsers that support it.

Validation with Regular Expressions


It validates text by determining whether or
not it matches a specific pattern.
Emails, Phone numbers and File names
Regular expressions have appeared in
countless other languages.
All regular expressions consist of two kinds of
characters: literals and metacharacters.

Literals - represent a specific defined


character.
Metacharacters provide the true secret to
unlocking the full power of regular
expressions.(example DEL *.*)

Regular Expressions Characters


* - Zero or more occurrences of the previous
character or subexpression. For example, 7*8
matches 7778 or just 8.
+ - One or more occurrences of the previous
character or subexpression. For example, 7+8
matches 7778 but not 8.
( ) - Groups a subexpression that will be
treated as a single element. For example,
(78)+ matches 78 and 787878.

CustomValidation Control
It has 3 important property
ControlToValidateThe ID of the form field being
validated.
TextThe error message displayed when validation
fails.
ClientValidationFunctionThe name of a client-side
function used to perform client-side validation.

It supports one event:


ServerValidateThis event is raised when the
CustomValidator performs validation

Manual Validation
Manual Validation can be done in 3 ways:
1) Use your own code to verify values. In this case, you
wont use any of the ASP.NET validation controls.
2) Disable the EnableClientScript property for each
validation control. This allows an invalid page to be
submitted, after which you can decide what to do with
it depending on the problems that may exist.
3) Add a button with CausesValidation set to false. When
this button is clicked, manually validate the page by
calling the Page.Validate() method.

You might also like