Upload
adnan-amin
View
231
Download
0
Embed Size (px)
Citation preview
8/6/2019 Lesson 11 Valid a Tors
1/22
Web Engineering-II
Using ASP.net
By
Adnan Amin
Lecturer / Software Programmer
Information and Communication Technology
( ICT Dept NIMA )
Adnan Amin (Lecturer / Programmer) 1
8/6/2019 Lesson 11 Valid a Tors
2/22
Types of Validator Controls0 There are six validator controls which are the following:
1. RequiredFieldValidator
2. RangeValidator
3. CompareValidator
4. RegularExpressionValidator
5. CustomValidator6. ValidationSummary
Adnan Amin (Lecturer / Programmer) 2
8/6/2019 Lesson 11 Valid a Tors
3/22
Six validation controls with descriptionSix validation controls with description
Validation Server Control Description
RequiredFieldValidatorEnsures that the user does not skip a form entry field.
CompareValidator
Allows for comparisons between the users input and another
item using a comparison operator (equals, greater than, less
than, and so on).
RangeValidator
Checks the users input based upon a lower- and upper level
range of numbers or characters.
RegularExpressionValidator
Checks that the users entry matches a pattern defined by a
regular expression. This is a good control to use to check e-mail
addresses and phone numbers.
CustomValidatorChecks the users entry using custom-coded validation logic.
ValidationSummaryDisplays all the error messages from the validators in one specific
spot on the page.Adnan Amin (Lecturer / Programmer) 3
8/6/2019 Lesson 11 Valid a Tors
4/22
1. The RequiredFieldValidator Control
0 The RequiredFieldValidator control simply checks to see if
something was entered into the HTML form element.
0 It is a simple validation control, but it is one of the most
frequently used.
0 You must have a RequiredFieldValidator control for each
form element on which you wish to enforce a value-
requiredrule where the user must enter a value (not leave
as blank).
Adnan Amin (Lecturer / Programmer) 4
8/6/2019 Lesson 11 Valid a Tors
5/22
Example: Validation using textbox control.
0 Add the following controls and set their properties;
Control name Properties Values
Label Text Your Name:
Textbox1RequiredFieldValidator1 ControlToValidate Textbox1
ErrorMessage * Your Name is required.
Your Name:
* RequiredFieldValidator1
textbox1
Codes on next slide.
Adnan Amin (Lecturer / Programmer) 5
8/6/2019 Lesson 11 Valid a Tors
6/22
Codes for Validation using textbox control.
The first property to look at is the ErrorMessage property.
This property is the value that is shown to the end user via the Web page if
the validation fails.
The second property to look at is the ControlToValidate property.
This property is used to make an association between this validation server
control and the ASP.NET form element (control e.g textbox) that requires the
validation. Adnan Amin (Lecturer / Programmer) 6
8/6/2019 Lesson 11 Valid a Tors
7/22
2. The CompareValidator Server Control
The CompareValidatorcontrol allows you to make
comparisons between two form elements.
For example:
You can specify that a form elements value must be an
integer and greater than number or less than a specified
number.
You can also state that values must be strings, dates, or other
data types.
Adnan Amin (Lecturer / Programmer) 7
8/6/2019 Lesson 11 Valid a Tors
8/22
Add two textbox controls on to web form
0 Add the following controls and set their properties;
Control name Properties Values
Label Text Your Name:
Textbox1
RequiredFieldValidator1 ControlToValidate Textbox1
ErrorMessage * Password is required.
CompareValidator1 ControlToValidate TextBox2
ControlToCompare Textbox1
ErrorMessage * Password is not matched
Password:
* RequiredFieldValidator1
Re-Password:
* CompareValidator1
textbox1
Codes on next slide.
textbox2Adnan Amin (Lecturer / Programmer) 8
8/6/2019 Lesson 11 Valid a Tors
9/22
Codes forCodes for CompareValidatorCompareValidator Control.Control.
Password:
Re-enter password:
8/6/2019 Lesson 11 Valid a Tors
10/22
The Operator property can take one of the following values:The Operator property can take one of the following values:
0 Equal
0NotEqual
0 GreaterThan0 GreaterThanEqual
0 LessThan
0 LessThanEqual
0 DataTypeCheck
Age:
Adnan Amin (Lecturer / Programmer) 1 0
8/6/2019 Lesson 11 Valid a Tors
11/22
3. The RangeValidator Server Control
0 The RangeValidator control is quite similar to that of theCompareValidator control,
0 but it makes sure that the end user value or selection provided
is between a specified range as opposed to being just greaterthan or less than a specified constant.
0 For an example of this, go back to the text-box element that
asks for the age of the end user and performs a validation onthe value provided.
Adnan Amin (Lecturer / Programmer) 11
8/6/2019 Lesson 11 Valid a Tors
12/22
Example: Validation using textbox control.
0 Add the following controls and set their properties;Control name Properties Values
Label Text Age:
Textbox1RangeValidator ControlToValidate Textbox1
ErrorMessage * Need age b/w 20-50
Type Integer
MaximumValue 20
MinimumValue 50
Age:
* RangeValidator1textbox1
Codes on next slide.
Adnan Amin (Lecturer / Programmer) 12
8/6/2019 Lesson 11 Valid a Tors
13/22
Code/Example for RangeValidator
Age:
Adnan Amin (Lecturer / Programmer) 13
8/6/2019 Lesson 11 Valid a Tors
14/22
4. The RegularExpressionValidator Server Control
0 you can check a users input based on a pattern that youdefine using a regular expression.
0 This means that you can define a structure that a users input
will be applied against to see if its structure matches the one
that you define.0 The user defined structure may be zip code , telephone
number, email address and URL etc.
Adnan Amin (Lecturer / Programmer) 14
8/6/2019 Lesson 11 Valid a Tors
15/22
Validation for an e-mail address using
RegularExpression
Control name Properties Values
Label Text Enter Your Email Address:
Textbox1
RegularExpression ControlToValidate Textbox1
ErrorMessage * Enter valid email address
ValidationExpression \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Age:
* RangeValidator1textbox1
Codes on next slide.
Adnan Amin (Lecturer / Programmer) 15
8/6/2019 Lesson 11 Valid a Tors
16/22
Code/Example for RegularExpression
Enter Your Email Address:
Adnan Amin (Lecturer / Programmer) 16
8/6/2019 Lesson 11 Valid a Tors
17/22
Regular Expression Editor
0 Visual Studio 2005 makes it alittle easier to use regularexpressions by introducingthe Regular ExpressionEditor. This editor provides afew commonly used regularexpressions that you mightwant to apply to your
RegularExpressionValidator.
Adnan Amin (Lecturer / Programmer) 17
8/6/2019 Lesson 11 Valid a Tors
18/22
6. The ValidationSummary Server Control
0 The ValidationSummary control is not a control that performs
validations on the content input into your Web forms.
0 Instead, this control is the reporting control, which is used by
the other validation controls on a page.
0
You can use this validation control to consolidate errorreporting for all the validation errors that occur on a page
instead of leaving this up to each and every individual
validation control.
Adnan Amin (Lecturer / Programmer) 18
8/6/2019 Lesson 11 Valid a Tors
19/22
Add the following controls on web form and set the following
properties accordingly.Controls Name Properties Name Properties Values.
Label1 Text First Name
Label2 Text Last Name
Button1 Text Send
RequiredFieldValidator1
ErrorMessage * First name is required.ControlToValidate Textbox1ValidationGroup Abc (or any other name)
RequiredFieldValidator2
ErrorMessage * Last name is required.ControlToValidate Textbox2ValidationGroup Abc
ValidationSummary1
ShowSummary or ShowMessageBox TrueValidationGroup1 Abc
Button1
Text SendCausesValidation
True
Adnan Amin (Lecturer / Programmer) 19
8/6/2019 Lesson 11 Valid a Tors
20/22
First Name: [requiredFieldValidator1]
Last Name: [requiredFieldValidator2]
Send ValidationSummary
Arrange all the controls on web form in given layout
Output Design View using asp.net
Adnan Amin (Lecturer / Programmer) 2 0
8/6/2019 Lesson 11 Valid a Tors
21/22
Codes:
First Name:
Last Name:
Adnan Amin (Lecturer / Programmer) 21
8/6/2019 Lesson 11 Valid a Tors
22/22
Thank You!0 References
0 Adnans lecture material
0 Professional ASP.net by Wrox team0 www.geoamins.com
Adnan Amin (Lecturer / Programmer) 22