Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

ASP .NET: Validating User Input with C#

By Exforsys | on March 4, 2005 |
ASP.NET

This tutorial covers Validating User Input with C# covers Overview of ASP.NET Validation Controls , Using the Simple Validators , Using the Complex Validators and Summarizing Results with the Validation Summary Control.

In this tutorial we will see the validation controls. The purpose of the validation controls is to validate the user input. Asp.net provides the developer with different types of validation controls. One most important point to note is that the validation is done on the client side as well as on the server side. You can always turn the validation on the client side off using the enable client side property to false. Lets see the difference between the client side validation and the server side validation.

Client side validation

When you place validation code on the client side, validation does not require a postback operation and provides fast responses to the user. However, because the validation code is outside the Web Server, it might be possible for the client to spoof the Web Server with invalid data. The addition to this, client-side validation requires the client to be capable of running scripts. That might be an issue with old browsers and some new browsers in which users turn off script execution thinking that scripts are unsafe. This client-side validation should never be used as the only validation technique to validate data on a Web page.

Server Side Validation

When the validation code is placed the server side, the process of validation might be slow because a form might involve multiple roundtrips to the Web Server before all the data is validated. On the other hand, because the Web Server is performing all the validation, you can trust the validated data. Server-side validation works well with even primitive browsers because it does not assume any specific browser capabilities.

(MCAD/MCSD Developing and implementing Web Applications with Visual C# .NET and Visual Studio. NET)

Required field validator is used for required fields. Fields that cannot be left empty is validated using the required field validator. A good example will be a TextBox which is used for taking the username as the input. Since the user must enter the username in the TextBox in order to access the website. The required field validator can be used with the TextBox control to validate the input. If user does not enter any data in the TextBox than an error message is displayed and further processing of the request will be stopped. In order to set the Required validator on the TextBox control just drag and drop the validator on the webform and set its ControltoValidate property to the TextBox id you want to validate.

  1. private void Button1_Click(object sender, System.EventArgs e) 
  2. { 
  3. if(Page.IsValid == true) 
  4. { 
  5. // Do the processing here 
  6. } 
  7. }

The Page.IsValid property is true if the page does not have any error messages to display. The Required field validator performs the validation first on the client side and than on the server side.

{mospagebreak title=Regular Expression Validator}

Regular Expression Validator is used to check the user input against some built in regular expressions. The control provides the RegularExpression collection property that can be used to select the desired Regular Expression. Some of the built in expressions are Email, postal code, US telephone number and many more. If the user input does not match the regular expression expected an error will occur and the request for the resources will not be authorized unless the user corrects the input data.

Apart from the regular expressions that ship with the Microsoft.net framework you can also make your own regular expressions using the using System.Text.RegularExpressions; namespace.

{mospagebreak title=Range Validator}

The Range Validator control is used to check whether the input control contains value in the specified range. You can check the range of values against different data types such as String, Date, Integer and so on.

The two most important properties of the range validator control is the Maximum value and the minimum value. The range can be used when you need to restrict the user data. Suppose you want the user to enter the number in the TextBox which is between 20 and 25 you can use range validator mimimum and maximum properties to restrict the user input.

  1. if(Page.IsValid == true) 
  2. { 
  3. RangeValidator1.MinimumValue = 20; 
  4. RangeValidator1.MaximumValue = 25; 
  5. }

{mospagebreak title=Compare Validator}

The compare validator control is used to compare the input server control’s value. The compare validator can be used to compare against a value or another control. If both the ControlToCompare and ValueToCompare properties are set for a CompareValidator control, the ControlToCompare property takes precedence.

A good use of the compare validator is to check whether the passwords entered by the user in the two TextBoxes while registering for a website are same or not. This validator also performs validation on the client side as well as the server side. So, no postback will be required when doing the comparison and hence resources will be saved and performance will increase.

{mospagebreak title=Custom Validator}

Custom Validators can be used to make your own custom validation expressions. You can find many free regular expressions on the website www.Regexlib.com. Here is a small example of the Custom Validator Control.

  1. // This is the server side validation 
  2. // Double click the Custom Validator and write this code 
  3. { 
  4. string strPalindrome = args.Value; 
  5. string strReverse = ""; 
  6. // Reverse the string 
  7. for(int intI = strPalindrome.Length -1; intI>=0; intI--) strReverse = strReverse + strPalindrome[intI]; 
  8. if(strReverse == strPalindrome) 
  9. { 
  10. args.IsValid = true; 
  11. } 
  12. else 
  13. {
  14. args.IsValid = false; 
  15. } 
  16. }

This validator simply checks that if the string entered is palindrome or not. (Developing and Implementing Web Applications, Kalani).

{mospagebreak title=Validation Summary Control}

The validation summary control is used to present the user with the summary of the errors that has occurred on the page. Its a good way of displaying all the errors at one place so that user does not have trouble time finding them. Validation Summary control gives the user the feature to display the error in the form of bulleted list or numbers.

I hope you enjoyed the Tutorial. Happy programming !

« « Oracle 9i Exception Handling
Microsoft Analysis Services Installation » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • Managing State with ASP.NET and C#

    May 3, 2005 - 0 Comment
  • ASP.NET with C# Training Launch

    February 19, 2005 - 0 Comment
  • Caching in ASP.NET

    May 9, 2005 - 0 Comment
  • Introduction to ASP.NET with C#

    February 20, 2005 - 0 Comment
  • Configuring and Deploying ASP.NET Applications

    May 14, 2005 - 0 Comment
  • ASP.NET with C# Training Course Outline

    February 19, 2005 - 0 Comment
  • Securing ASP.NET Applications with C#

    May 14, 2005 - 0 Comment
  • ASP.NET Web Forms Controls

    February 26, 2005 - 0 Comment
  • Using Rich Server Controls with C#

    March 12, 2005 - 0 Comment
  • Accessing Data with C#

    March 19, 2005 - 0 Comment
  • Securing ASP.NET Applications with C#

    May 14, 2005 - 0 Comment
  • Configuring and Deploying ASP.NET Applications

    May 14, 2005 - 0 Comment
  • Caching in ASP.NET

    May 9, 2005 - 0 Comment
  • Managing State with ASP.NET and C#

    May 3, 2005 - 0 Comment
  • ASP .NET Migration and Interoperability

    April 24, 2005 - 0 Comment
  • Creating and consuming XML Web Services with C#

    April 14, 2005 - 0 Comment
  • Managing Data with ADO.NET DataSets and C#

    April 8, 2005 - 0 Comment
  • ASP.NET Using the DataList and Repeater, Datagrid Controls

    March 26, 2005 - 0 Comment
  • Accessing Data with C#

    March 19, 2005 - 0 Comment
  • Using Rich Server Controls with C#

    March 12, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Securing ASP.NET Applications with C#
  • Configuring and Deploying ASP.NET Applications
  • Caching in ASP.NET
  • Managing State with ASP.NET and C#
  • ASP .NET Migration and Interoperability

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT