Tuesday, February 2, 2010

Validation in CS Page file

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace PortalUtilities
{
    ///
    /// This Class is used for Validation Check for the controls
    ///
  
    public class Validation
    {
        #region IsNaturalNumber Method
       
        ///
        /// 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);
        }
        #endregion IsNaturalNumber Method

        #region IsWholeNumber Method
       
        ///
        /// Function to test for Positive Integers with zero inclusive
        ///

        ///
        ///       
        public bool IsWholeNumber(string strNumber)
        {
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strNumber);
        }
        #endregion IsWholeNumber Method
       
        #region IsInteger Method
       
        ///
        /// 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);
        }
        #endregion IsInteger Method

        #region IsPositiveNumber Method
       
        ///
        /// 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);
        }
        #endregion IsPositiveNumber Method

        #region IsNumber Method
       
        ///
        /// 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 + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
                   !objTwoDotPattern.IsMatch(strNumber) &&
                   !objTwoMinusPattern.IsMatch(strNumber) &&
                   objNumberPattern.IsMatch(strNumber);
        }
        #endregion IsNumber Method

        #region IsAlpha Method
       
        ///
        /// Function To test for Alphabets.
        ///

        ///
        ///       
        public bool IsAlpha(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlpha Method

        #region IsAlphaWithBlankChar Method
       
        ///
        /// Function To test for Alphabets and Blank Char.
        ///

        ///
        ///      
        public bool IsAlphaWithBlankChar(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z ]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithBlankChar Method

        #region IsAlphaWithApostrophe Method
       
        ///
        /// Function To test for Alphabets. with Apostrophe s('s)
        ///

        ///
        ///       
         public bool IsAlphaWithApostrophe(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z']");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithApostrophe Method

        #region IsAlphaWithApostropheAndBlank Method
       
        ///
        /// Function To test for Alphabets. with Apostrophe s('s)and Blank Char
        ///

        ///
        ///
        /// Dhrityman Mukherjee
        /// 05-DEC-2007
        public bool IsAlphaWithApostropheAndBlank(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z' ]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithApostropheAndBlank Method

        #region IsAlphaNumeric Method
       
        ///
        /// Function to Check for AlphaNumeric.
        ///

        ///
        ///      
        public bool IsAlphaNumeric(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumeric Method

        #region IsAlphaNumericWithBlankChar Method

        ///
        /// Function to Check for Alpha Numeric with Blank Char.
        ///

        ///
        ///      
        public bool IsAlphaNumericWithBlankChar(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithBlankChar Method

        #region IsAlphaNumericWithApostrophe Method

        ///
        /// Function to Check for Alpha Numeric with Apostrophe.
        ///

        ///
        ///     
        public bool IsAlphaNumericWithApostrophe(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9']");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithApostrophe Method

        #region IsAlphaNumericWithApostropheBlankChar Method

        ///
        /// Function to Check for Alpha Numeric with Apostrophe and Blank Char.
        ///

        ///
        ///       
        public bool IsAlphaNumericWithApostropheBlankChar(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9' ]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithApostropheBlankChar Method

        #region IsValidFileFolderPathSyntex Method
        ///
        ///  Function to Check for any File or folder path syntex is valid or not?
        /// (specially for WidgetsMaster_ae.aspx to check txtUserControlPath.Text
        /// and for ModuleItemsMaster_ae.aspx to check txtAspxPage.Tex and etc.).
        ///

        ///
        ///    
        public bool IsValidFileFolderPathSyntex(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@.$():~/ -]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsValidFileFolderPathSyntex Method

        #region IsSqlInjectionFree Method

        ///
        /// Function to Check for Sql injection.
        ///

        ///
        ///      
        public bool IsSqlInjectionFree(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@.$():',/ -]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsSqlInjectionFree Method

        #region IsValidPassword Method
        ///
        /// Function to Check Password type Alpha numiric with five special char only(@, &, $, ! And #)
        /// as per user requirement and also work to checl for Sql injection.
        ///

        ///
        ///      
        public bool IsValidPassword(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@&$!#]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsValidPassword Method

        #region IsValidPasswordFormat Method
        ///
        /// Function to Check Password type Alpha numiric with five special char only(@, &, $, ! And #)
        /// as per user requirement and also work to check for Sql injection.
        /// length should be between 7 to 12 char, at least one number and one Alpha char should be  present.
        ///

        ///
        ///      
        public bool IsValidPasswordFormat(string strToCheck)
        {
            if (strToCheck.Length >= 7 && strToCheck.Length <= 12)
            {
                Regex objAlphaPattern = new Regex("[a-zA-Z]");
                Regex objNumericPattern = new Regex("[0-9]");
                return (objAlphaPattern.IsMatch(strToCheck) && objNumericPattern.IsMatch(strToCheck));
            }
            else
                return false;
        }
        #endregion IsValidPasswordFormat Method

    }
}

No comments:

Post a Comment