Chapter 9


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 9

  1. 1. • 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
  2. 2. 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
  3. 3. ASP.NET provides five validator controls. Four aretargeted at specific types of validation, while the fifthallows you to apply custom validation routines
  4. 4. All the validation controls are found in the System.Web.UI.WebControlsnamespace and inherit from the BaseValidator class.Property DescriptionControlToValidate Identifies the control that is to be validatedError Message If validation fails, the validator control can display a text message set by this propertyForeColor By changing the ForeColor, you can make the error message stand outDisplay Dynamic, Static or NoneIsValid Returns true or false depending on whether it succeeded or failedEnabled When set to false, automatic validation will not be performed for this control when the page is submittedEnableClientScript If set to true, ASP.NET will add JavaScript and DHTML code to allow client-side validation on browsers that support it.
  5. 5. 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
  6. 6. In some modern browsers, ASP.NET automaticallyadds JavaScript code for client-side validation. In thiscase, when the user clicks a CausesValidation button,the same error messages will appear without the pageneeding to be submitted and returned from the server.This increases the responsiveness of your web page.However, even if the page validates successfully onthe client side, ASP.NET still revalidates it when it’sreceived at the server.
  7. 7. 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.
  8. 8. 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
  9. 9. 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.
  10. 10. 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.
  11. 11. 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.