12
Web Application and Development Digital Media Departmen Unit Credit Value : 4 Essential Learning time : 120 hours s [email protected] Digital Media Department Politeknik Brunei VALIDATION ASP C#

Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours [email protected] Digital Media

Embed Size (px)

Citation preview

Web Application and Development

Digital Media Department

Unit Credit Value : 4EssentialLearning time : 120 hours

[email protected] Media DepartmentPoliteknik Brunei

VALIDATION ASP C#

Validating User Input with Validation Controls

• ASP.NET offers validation controls for validating user input

• Three of the most commonly used validation controls:– RequiredFieldValidator control– CompareValidator control– RegularExpressionValidator control

ASP.NET Programming with C# and SQL Server, First Edition 2

RequiredFieldValidator Control

• RequiredFieldValidator control: ensures that a value is entered into a specified field on a Web form– Text property: contains the error message that will appear if the field is

empty when the user tries to submit the form– ControlToValidate property: the field that is to be validated

ASP.NET Programming with C# and SQL Server, First Edition 3

RequiredFieldValidator Control (cont’d.)

ASP.NET Programming with C# and SQL Server, First Edition 4

Figure 4-10 BMI program with RequiredFieldValidator controls

CompareValidator Control• CompareValidator control: verifies that the

entered value is a specific data type– Text property: the message to appear if the data

type of the entered value is incorrect– ControlToValidate property: the form field to be

validated– ControlToCompare property: another field against

which the field being validated is to be compared– Type property: the data type that must be used– Operator property: the comparison type to perform– ValueToCompare property: the value to be used in

the comparison testASP.NET Programming with C# and SQL Server, First Edition 5

CompareValidator Control (cont’d.)

ASP.NET Programming with C# and SQL Server, First Edition 6

Table 4-5 Comparison types of the Operator property

RegularExpressionValidator

7

A regular expression is a powerful pattern matching language that can be used to identify simple and complex characters sequence that would otherwise require writing code to perform.

Using RegularExpressionValidator server control, you can check a user's input based on a pattern that you define using a regular expression. 

It is used to validate complex expressions. These expressions can be phone number, email address, zip code and many more. Using Regular Expression Validator is very simple. Simply set the ValidationExpression property to any type of expression you want and it will validate it. 

8

Validation Expression

Field Expression Format Samples Description

Name ^[a-zA-Z''-'\s]{1,40}$ John DoeO'Dell

Validates a name. Allows up to 40 uppercase and lowercase characters and a few special characters that are common to some names. You can modify this list.

Social Security Number ^\d{3}-\d{2}-\d{4}$ 111-11-1111 Validates the format, type, and length of the supplied input field. The input must consist of 3 numeric characters followed by a dash, then 2 numeric characters followed by a dash, and then 4 numeric characters.

Phone Number ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$

(425) 555-0123425-555-0123425 555 01231-425-555-0123

Validates a U.S. phone number. It must consist of 3 numeric characters, optionally enclosed in parentheses, followed by a set of 3 numeric characters and then a set of 4 numeric characters.

E-mail ^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$

[email protected] Validates an e-mail address.

URL ^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

http://www.microsoft.com Validates a URL

9

Validation Expression

ZIP Code ^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$

12345 Validates a U.S. ZIP Code. The code must consist of 5 or 9 numeric characters.

Password (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$

  Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.

Non- negative integer ^\d+$ 0986

Validates that the field contains an integer greater than zero.

Currency (non- negative) ^\d+(\.\d\d)?$ 1.00 Validates a positive currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point. For example, 3.00 is valid but 3.1 is not.

Currency (positive or negative) ^(-)?\d+(\.\d\d)?$ 1.20 Validates for a positive or negative currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point.

10

Validation Webform Sample

ASP.NET Programming with C# and SQL Server, First Edition

11

Summary

ASP.NET Programming with C# and SQL Server, First Edition

Validation Control Description

RequiredFieldValidation Makes an input control a required field

CompareValidator Compares the value of one input control to the value of another input control or to a fixed value

RangeValidator Checks that the user enters a value that falls between two values

RegularExpressionValidator Ensures that the value of an input control matches a specified pattern

CustomValidator Allows you to write a method to handle the validation of the value entered

ValidationSummary Displays a report of all validation errors occurred in a Web page

The below table describes the controls and their work:

ContactFor Tutorial and NotesCikguhadi.com

[email protected] Media DepartmentPoliteknik Brunei

Welcome to WADT