• Understanding Validation• Validation Controls• BaseValidator Class Properties• Validator Specific Properties• Validating with Regular Expressions• Regular Expression Characters• Commonly used Regular Expressions• Validation Summary Control• Manual Validation• Validation Groups
Validation means ensuring that the data inserted into anapplication satisfies defined formats and other input criteria.Some possible mistakes a user can make:• Users might ignore an important field and leave it blank.• Users might try to type a short string of nonsense to circumvent a required field check.• Enter a nonnumeric character in a number field.• Malicious users might try to exploit a weakness in your code by entering carefully structured wrong values
ASP.NET provides five validator controls. Four aretargeted at specific types of validation, while the fifthallows you to apply custom validation routines
One of ASP.NET’s most powerful validation controls is theRegularExpressionValidator, which validates text by determiningwhether it matches a specific pattern.333sddds represents any whitespace character (such as a space or tab). drepresents any digitYou can use the plus (+) sign to represent a repeated character. Forexample, 5+7 means “one or more occurrences of the character 5,followed by a single 7.” The number 57 would match, as would555557The following expression would match any word that starts with aletter from a to f, contains one or more “word” characters (letters),and ends with ing—possible matches include acting and developing.[a-f]w+ing
What happens when the user clicks a button :• If CausesValidation is false, ASP.NET will ignore thevalidation controls, the page will be posted back, andyour event handling code will run normally.• If CausesValidation is true (the default), ASP.NET willautomatically validate the page when the user clicksthe button. It does this by performing the validation foreach control on the page. If any control fails tovalidate, ASP.NET will return the page with some errorinformation. Your click event handling code may ormay not be executed.Use IsValid property in a conditional logic and writeevent handling code inside it.
Accommodates all the error messages from all thevalidation controls at a suitable location normally at thebottom of the page.Error messages from all the validators are hided bysetting their Display property to None.When you run the page, you won’t see any dynamicmessages as you enter invalid information andtab to a new field. However, when you click the OKbutton, the ValidationSummary will appear with a listof all error messages.It automatically retrieves the value of the ErrorMessageproperty from each validator
In some cases, you’ll want to display a full message inthe summary and some sort of visual indicator next tothe offending controlYou can use this technique with the help of the Textproperty of the validators. Ordinarily, Text is left empty,and the validator doesn’t show any content in the webpage. However, if you set both Text and ErrorMessage,the ErrorMessage value will be used for the summarywhile the Text value is displayed in the validator.Of course, you’ll need to make sure you aren’t alsosetting the Display property of your validator to None.
Manual validation is when you disable validation and performthe work on your own. This allows you to create a specializederror message of your own.You can create manual validation in one of three ways:I. Use your own code to verify values. In this case, you won’t use any of the ASP.NET validation controls.II. Disable the EnableClientScript property for each validation control. This allows an invalid page to be submitted, after which you can decide what to do with itdepending on the problems that may exist.III. Add a button with CausesValidation set to false. When this button is clicked, manually validate the page by calling the Page.Validate() method. Then examine the IsValid property, and decide what to do.
Every control that provides a CausesValidation propertyalso includes the ValidationGroup property.In some situations you might have several distinct groups ofcontrols, possibly in separate panels and you may want toperform validations separately.To create a validation group, you need to set theValidationGroup property of every control in the samelogical group with the same descriptive string.