ASP.NET Validation Controls and Best Practices for Data Integrity

asp net validation controls n.w
1 / 15
Embed
Share

Learn the importance of checking the validity of user input data in ASP.NET applications and the different methods of validation. Explore server-side versus client-side validation, common validation controls like RequiredFieldValidator and CompareValidator, and key properties of validation controls.

  • ASP.NET Validation Controls
  • Data Integrity
  • User Input Validation
  • Server-Side Validation
  • Client-Side Validation

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ASP.NET Validation Controls Validating user input Naveen Papagari Presentation Id #13

  2. Goals Check validity of data from the end user Why To maintain data integrity How Check user input on the client side or on the server side Examples Is the name Textbox empty? Does the email Textbox contain a structurally valid email address?

  3. Client Side Vs Server Side Server-side validation Client-side validation Done on the server Uses C# Requires HTTP request + response Done in the browser Uses JavaScript Does not require HTTP request + response

  4. ASPNET Validation Controls RequiredFieldValidator Input field cannot be empty CompareValidator Compare between user inputs using =, >, etc. RangeValidator Minimum < input < maximum RegularExpressionValidator Check the entry matches a pattern defined by the regular expression CustomValidator Make your own validator ValidationSummary Displays all error messages from validators in one spot

  5. Common Properties of Validation Controls ControlToValidate The control to be validated ErrorMessage The message used in the ValidationSummary Text The error message used in the validation control CssClass Style appearance of the messages

  6. Required Field Validator The RequiredFieldValidator control ensures that the required field is not empty. It is generally tied to a text box to force input into the text box. The syntax for the control: <asp:RequiredFieldValidator ID="rfvcandidate" runat="server" ControlToValidate="ddlcandidate" ErrorMessage="Please choose a candidate" InitialValue="Please choose a candidate"> </asp:RequiredFieldValidator>

  7. Compare Validator The CompareValidator control compares a value in one control with a fixed value, or, a value in another control. It has the following specific properties: The basic syntax for the control: <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"> </asp:CompareValidator>

  8. Range Validator The RangeValidator control verifies that the input value falls within a predetermined range. It has three specific properties: The syntax for the control: <asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer"> </asp:RangeValidator>

  9. Regular Expression Validator The RegularExpressionValidator allows validating the input text by matching against a pattern against a regular expression. The regular expression is set in the ValidationExpression property. The following table summarizes the commonly used syntax constructs for regular expressions: <asp:RegularExpressionValidator ID="RegExpVal2" runat="server" ErrorMessage = "Please Enter Valid Email ID ValidationGroup="vgSubmit" ControlToValidate="txtEmail" CssClass="requiredFieldValidateStyle" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>

  10. Custom Validator The CustomValidator control allows writing application specific custom validation routines for both the client side and the server side validation. The server side validation routine should be written in any .Net language, like C# or VB.Net. The basic syntax for the control: <asp:CustomValidator ID="CustomValidator1" ClientValidationFunction=.cvf_func. ErrorMessage="CustomValidator"></asp:CustomValidator> runat="server"

  11. Validation Summary The ValidationSummary control does not perform any validation but shows a summary of all errors in the page. The summary displays the values of the ErrorMessage property of all validation controls that failed validation. Using Page.IsValid property we can check for page errors. When there are no errors IsValid returns true and user can proceed to next page. if (Page.IsValid) { //validation complete proceed } The syntax for the control: <asp:ValidationSummary ID="ValidationSummary1" DisplayMode = "BulletList" HeaderText="Errors:" /> runat="server" ShowSummary = "true"

  12. Base Validator Class

  13. Validation Group First Problem: First Problem: when I click the Clear when I click the Clear button, Form validation still happens. button, Form validation still happens. When I click the clear button, I just want When I click the clear button, I just want to clear the textboxes in the to clear the textboxes in the Registration section. Validations doesn't Registration section. Validations doesn't make any sense here. So, how do I make any sense here. So, how do I prevent validation from happening? prevent validation from happening? Answer: Answer: CausesValidation= False CausesValidation= False Second problem Second problem = when I click the Login button, only fields in the Login section = when I click the Login button, only fields in the Login section (Username & Password) needs to be validated. Along the same lines when I click the (Username & Password) needs to be validated. Along the same lines when I click the "Register" button, only fields in the Registration section(Email, Username, Password and "Register" button, only fields in the Registration section(Email, Username, Password and ConfirmPassword ConfirmPassword) needs to validated. If we don't use validation groups, then by default, ) needs to validated. If we don't use validation groups, then by default, whenever, you click any button, all the validation controls on the page get validated. whenever, you click any button, all the validation controls on the page get validated. So, when you click the login button, and if you want only, the fields in the Login So, when you click the login button, and if you want only, the fields in the Login section(Username and Password) to be validated, then set, the section(Username and Password) to be validated, then set, the Validationgroup of the validation controls and the login button control to the same group name. Use a of the validation controls and the login button control to the same group name. Use a different group name for the validation controls and register button, in the registration different group name for the validation controls and register button, in the registration section. section. Validationgroup property property

Related


More Related Content