Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital Media Department Politeknik 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 Edition2
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 Edition3
RequiredFieldValidator Control (cont’d.) ASP.NET Programming with C# and SQL Server, First Edition4 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 test ASP.NET Programming with C# and SQL Server, First Edition5
CompareValidator Control (cont’d.) ASP.NET Programming with C# and SQL Server, First Edition6 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, 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.
Validation Expression 8 FieldExpressionFormat SamplesDescription Name^[a-zA-Z''-'\s]{1,40}$John Doe O'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}$ 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) 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. !#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a- zA- 9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA- Z]{2,6}))$ an address. URL^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a- zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\- \.\?\,\'\/\\\+&%\$#_]*)?$ a URL
Validation Expression 9 ZIP Code^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA- Z]\d[a-zA-Z] \d[a-zA-Z]\d)$ 12345Validates 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+$0 986 Validates that the field contains an integer greater than zero. Currency (non- negative)^\d+(\.\d\d)?$1.00Validates 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.20Validates for a positive or negative currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point.
Validation Webform Sample ASP.NET Programming with C# and SQL Server, First Edition10
Summary ASP.NET Programming with C# and SQL Server, First Edition11 Validation ControlDescription RequiredFieldValidationMakes an input control a required field CompareValidatorCompares the value of one input control to the value of another input control or to a fixed value RangeValidatorChecks that the user enters a value that falls between two values RegularExpressionValidatorEnsures that the value of an input control matches a specified pattern CustomValidatorAllows you to write a method to handle the validation of the value entered ValidationSummaryDisplays a report of all validation errors occurred in a Web page The below table describes the controls and their work:
Contact For Tutorial and Notes Cikguhadi.com Digital Media Department Politeknik Brunei Welcome to WADT