Reusable validation methods in C# .Net

Discussion in 'ASP.NET' started by naimish, Sep 16, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    Introduction



    Many times we need to check or validations for numeric/string/Alphanumeric values.

    Background



    It provides some methods in C# .Net, which can be resue in your web or windows application.

    The code



    Code:
    using System;
    using System.Text.RegularExpressions;
    class Validation {
      public static void Main() {
        String strToTest;
        Validation objValidate = new Validation();
        Console.Write("Enter a String to Test for Alphabets:");
        strToTest = Console.ReadLine();
        if (objValidate.IsAlpha(strToTest)) {
          Console.WriteLine("{0} is Valid Alpha String", strToTest);
        } else {
          Console.WriteLine("{0} is not a Valid Alpha String", strToTest);
        }
      }
      // Function to test for Positive Integers.
      public bool IsNaturalNumber(String strNumber) {
        Regex objNotNaturalPattern = new Regex("[^0-9]");
        Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");
        return !objNotNaturalPattern.IsMatch(strNumber) && objNaturalPattern.IsMatch(strNumber);
      }
      // Function to test for Positive Integers with zero inclusive
      public bool IsWholeNumber(String strNumber) {
        Regex objNotWholePattern = new Regex("[^0-9]");
        return !objNotWholePattern.IsMatch(strNumber);
      }
      // Function to Test for Integers both Positive & Negative
      public bool IsInteger(String strNumber) {
        Regex objNotIntPattern = new Regex("[^0-9-]");
        Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
        return !objNotIntPattern.IsMatch(strNumber) &&
        objIntPattern.IsMatch(strNumber);
      }
      // Function to Test for Positive Number both Integer & Real
      public bool IsPositiveNumber(String strNumber) {
        Regex objNotPositivePattern = new Regex("[^0-9.]");
        Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
        Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
        return !objNotPositivePattern.IsMatch(strNumber) &&
        objPositivePattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber);
      }
      // Function to test whether the string is valid number or not
      public bool IsNumber(String strNumber) {
        Regex objNotNumberPattern = new Regex("[^0-9.-]");
        Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
        Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
        String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
        String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
        Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + trValidIntegerPattern + ")");
        return !objNotNumberPattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber) && !objTwoMinusPattern.IsMatch(strNumber) && objNumberPattern.IsMatch(strNumber);
      }
      // Function To test for Alphabets.
      public bool IsAlpha(String strToCheck) {
        Regex objAlphaPattern = new Regex("[^a-zA-Z]");
        return !objAlphaPattern.IsMatch(strToCheck);
      }
      // Function to Check for AlphaNumeric.
      public bool IsAlphaNumeric(String strToCheck) {
        Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
        return !objAlphaNumericPattern.IsMatch(strToCheck);
      }
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Hello, this is great article. I have blog and I thanks to say you thanks. Regards!
     
  4. technica

    technica New Member

    Joined:
    Dec 15, 2007
    Messages:
    107
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.technicaltalk.net
    You can also include validations for Email Address, Password Comparison etc.
     
  5. iamscottj

    iamscottj New Member

    Joined:
    Aug 18, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    US
    I think its pretty much straight forward. You can get various validations. I would suggest some kind of XML file where you can create dynamic validations like if someone edits the XML file in a suitable manner, he can use that validation in the application. And as technica suggested, I also believe that there must be EMail validation as its currently the most required validation.
     
  6. almedajohnson

    almedajohnson New Member

    Joined:
    Sep 17, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I loved this forum. As there is so many things to learn here. Thanks guys for maintaining such a valuable .net community
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice