Asp.NET Core Vaidation Controls

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)


Asp.NET Core Vaidation Controls. ASP.NET Validation Controls (Introduction). The ASP.NET validation controls can be used to validate data on the client, the server, or both Client-side validation is performed using automatically generated JavaScript - PowerPoint PPT Presentation

Transcript of Asp.NET Core Vaidation Controls


Asp.NET Core Vaidation ControlsSlide #ASP.NET Validation Controls (Introduction)The ASP.NET validation controls can be used to validate data on the client, the server, or bothClient-side validation is performed using automatically generated JavaScriptServer-side validation is performed using VB or C# code in the proper event handlerServer-side validation is always enabled while client-side validation is optionally enabledSlide #ASP.NET Validation Controls (Introduction)If client-side validation is enabled, we dont postback until the client thinks the values are validAlways validate on the server-side too to thwart those hackers doing a post on their ownSlide #ASP.NET Validation Controls (Server Processing Model 1)Validation controls apply their rules and validate a corresponding controlCall the Page.Validate() method to force the validation controls to executeThis is usually not necessaryTest Page.IsValid to determine whether the page has valid data or notSlide #ASP.NET Validation Controls (Server Processing Model 2)Page statesControls are initialized and populated when the page is loadedAfter the page loads, the controls are validated and the Page.IsValid property is setThe Page.Validators property contains results of each controls validatorEach item in the collection implements IValidator

Slide #Checking the Validators (Example)Display the validation messages:

For Each v In Page.Validators Response.Write(v.ErrorMessage)Next

foreach (Ivalidator v in Page.Validators){Response.Write(v.ErrorMessage)}Slide #ASP.NET Validation Controls (Concepts)Set the ControlToValidate property the control instance that will be validatedSet EnableClientScript to enable client-side validationASP will generate the necessary JavaScriptSet the ErrorMessage property to the error message that will appear in the validation controlSlide #ASP.NET Validation Controls (RequiredFieldValidator and RangeValidator)RequiredFieldValidator checks that a control has a valueRangeValidator checks that a controls value is within a valid rangeSet the MinimumValue and MaximumValue properties to the valid rangeA control with no valid is considered validUse with the RequiredFieldValidator to prevent thisSet the Type property to define the data type to be stored in the control to validateSlide #ASP.NET Validation Controls (CompareValidator)The CompareValidator validates that the values of two other control instances are the sameSet the ControlToValidate and ControlToCompare propertiesOptionally use a RequiredFieldValidator so that empty controls will not be validSlide #ASP.NET Validation Controls (CustomValidator)The CustomValidator control allows you to easily create custom and client script and server event handlersSet the ControlToValidate as usualSet the ClientValidationFunction to the name of the JavaScript function appearing on the clientServer-side validation is automatic You must write the code for the ServerValidate event handler

Slide #ASP.NET Validation Controls(CustomValidator)The client validation function must accept two argumentsThe first contains the object that fired the eventThe second contains the event dataSlide #ASP.NET Validation Controls(CustomValidator) (Example Client)function validateLength(src,args) { if (args.Value.length 6 Then args.IsValid = True Else args.IsValid = False End IfEnd SubSlide #Regular Expressions (Introduction)Regular expressions are used throughout computer science and programming languages as a means of pattern matchingPatterns are matched using a regular expression engineYou generally don't run the engine directlyDifferent implementations have different "flavorsSlide #Regular Expressions (Implementations)Some consider Perl 5 as the basis or standard for regular expressions.NET supports regular expressionsJavaScript supports regular expressionUNIX scripting languages support regular expressionsAnd of course XML supports themSlide #Regular Expressions (Literal Values)The most simple of regular expressions will match a literal valueBy default, regular expressions are case sensitiveThe regular expression "cat" contains three literal charactersIt will match the following patterns"concatenate""the cat ran away"Slide #Regular Expressions (Quantifiers)Quantifiers denote how many times a character or pattern can repeatQuantifiers (list)* (0 or more)+ (1 or more)? (0 or 1)Slide #Quantifiers (Example)1?5?1 can appear 0-1 times and 5 can appear 0-1 timesThis pattern also matches an empty string because 1 or 5 can appear 0 timesSlide #Quantifiers (Numeric Range)Quantifiers allow us to specify how many times a pattern can occurNumeric range quantifiers appear in curly braces{n} Must occur exactly n times{n,m} Must occur between n and m times{n,} Must occur n or greater times

Slide #Special Characters (Introduction)Some characters (*, ?, +, and others) have special meaning when creating regular expressionsThese characters are called metacharactersTo include these characters as literal characters in a regular expression, they must be escapedThe \ is the escape characterSlide #Special Characters (List)\n\r\t\\\|\-\^\?\*\+\{\}\(\)\[\]linefeedcarriage returntabThe backward slash \The vertical bar |The hyphen - The caret ^The question mark ?The asterisk *The plus sign +The open curly brace {The close curly brace }The open paren (The close paren )The open square bracket [The close square bracket ]

Slide #Special Characters (.)The dot (.) matches any character (except for the newline character)Thus, the following would match any character followed by the digit 0

.0Slide #Character Classes (List 1)\s matches a space\S matches any character that is not a space\d matches any digit\D matches any character that is not a digit\w matches a word\W matches any character sequence that is not a word

Slide #User Defined Character Classes (1)We can tell the regular expression engine to match a range of charactersSquare brackets surround the rangeOnly one character can appear in the rangeSlide #User Defined Character Classes (2)A hyphen can appear in a character class to denote a rangeMultiple ranges can appear in a character classThe order of ranges is not significantExamplesA name beginning with a letter followed by any sequence of upper-case or lower-chase charactersSlide #NegationThe caret "^" is the negation character when used in a character classA "q" followed by any sequence of characters that is not a "u"Slide #Start of Pattern and End of PatternAnchors are used to match a position"^" matches the start of a pattern"$" matches the end of the patternSlide #AlternationAlternation is basically a logical orThe vertical bar is the alternation symbol

Example:The following matches "cat" or "dog"

Slide #.NET and Regular ExpressionsThe System.Text.RegularExpressions namespace contains classes designed to match regular expressions against a stringA Match object is returned by a pattern-matching operationSlide #Testing a Regular ExpressionThe following statement runs a regular expression against a string and returns a Match:Dim Result As _ System.Text.RegularExpressions. _ MatchResult = _ System.Text.RegularExpressions. _ Regex.Match(txtString.Text, _ txtPattern.Text)Slide #