.NET validation controls validate user input on both the client-side using JavaScript and server-side. There are six main validation controls: RequiredFieldValidator, CompareValidator, RangeValidator, RegularExpressionValidator, CustomValidator, and ValidationSummary. Validation controls make validation convenient by allowing controls to be cut and pasted between pages and providing both client-side and server-side validation. They are flexible in that any type of input can be validated, including data types, constants, other controls, and database values.
1. .NET Validation Controls.NET Validation Controls
MacDonald Ch. 8MacDonald Ch. 8
MIS 324MIS 324
Professor SandvigProfessor Sandvig
2. TodayToday
What Validation controls doWhat Validation controls do
Benefits & LimitationsBenefits & Limitations
Six Validation ControlsSix Validation Controls
Implementation ExampleImplementation Example
3. What Validation Controls DoWhat Validation Controls Do
Each control:Each control:
– Validates user inputs on client-side using JavaScriptValidates user inputs on client-side using JavaScript
When possibleWhen possible
– Validates all inputs on serverValidates all inputs on server
– Writes client-side error message if invalidWrites client-side error message if invalid
4. Benefits and LimitationsBenefits and Limitations
BenefitsBenefits
– ConvenienceConvenience
Cut & pasteCut & paste
– Bomb-proofBomb-proof
Server-side validationServer-side validation
LimitationsLimitations
– Must remember to check on serverMust remember to check on server
Page.IsValid statementPage.IsValid statement
5. Six Validation ControlsSix Validation Controls
1.1. RequiredFieldValidatorRequiredFieldValidator
– Only validator that checks for NO entryOnly validator that checks for NO entry
1.1. CompareValidatorCompareValidator
– Compares input to: datatype, constant,Compares input to: datatype, constant,
another control, database value, etc.another control, database value, etc.
1.1. RangeValidatorRangeValidator
– Entry within a specified data rangeEntry within a specified data range
6. Six Validation ControlsSix Validation Controls
4.4. RegularExpressionValidatorRegularExpressionValidator
– Check format against a specific patternCheck format against a specific pattern
– E-mail address, phone number, zip code, etc.E-mail address, phone number, zip code, etc.
4.4. CustomValidatorCustomValidator
– Write own codeWrite own code
– Server- or client-sideServer- or client-side
4.4. ValidationSummaryValidationSummary
– Summarizes all errors on pageSummarizes all errors on page
7. ImplementationImplementation
Often need > 1 validator per inputOften need > 1 validator per input
– Only RequiredFieldValidator checks for empty fieldsOnly RequiredFieldValidator checks for empty fields
Page.IsValidPage.IsValid
– Check in event handlerCheck in event handler
if (!Page.IsValid) return;if (!Page.IsValid) return;
Validation GroupsValidation Groups
– Allow validation controls to be assigned to groupsAllow validation controls to be assigned to groups
– Page can have several groupsPage can have several groups
– Act independentlyAct independently
– Only group associated with postback event must have valid entriesOnly group associated with postback event must have valid entries
– Example:Example: No groupsNo groups, groups, groups
Example:Example:
– Required, Compare, Range,Required, Compare, Range,
& Custom (client- and server-side script)& Custom (client- and server-side script)
– Validation.aspx (source)Validation.aspx (source)
8. SummarySummary
Easy to useEasy to use
FlexibleFlexible
– Validate any type of inputValidate any type of input
Combines client- and server-sideCombines client- and server-side
validation in one controlvalidation in one control
Bomb-proof server-side validationBomb-proof server-side validation