Geeks With Blogs
Gaurav Taneja Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace PortalUtilities
{
    /// <summary>
    /// This Class is used for Validation Check for the controls
    /// </summary>  
    public class Validation
    {
        #region IsNaturalNumber Method
       
        /// <summary>
        /// Function to test for Positive Integers.
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        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
       
        /// <summary>
        /// Function to test for Positive Integers with zero inclusive
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>      
        public bool IsWholeNumber(string strNumber)
        {
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strNumber);
        }
        #endregion IsWholeNumber Method
       
        #region IsInteger Method
       
        /// <summary>
        /// Function to Test for Integers both Positive & Negative
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>    
        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
       
        /// <summary>
        /// Function to Test for Positive Number both Integer & Real
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>      
        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
       
        /// <summary>
        /// Function to test whether the string is valid number or not
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>     
        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
       
        /// <summary>
        /// Function To test for Alphabets.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>      
        public bool IsAlpha(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlpha Method

        #region IsAlphaWithBlankChar Method
       
        /// <summary>
        /// Function To test for Alphabets and Blank Char.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        public bool IsAlphaWithBlankChar(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z ]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithBlankChar Method

        #region IsAlphaWithApostrophe Method
       
        /// <summary>
        /// Function To test for Alphabets. with Apostrophe s('s)
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>      
         public bool IsAlphaWithApostrophe(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z']");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithApostrophe Method

        #region IsAlphaWithApostropheAndBlank Method
       
        /// <summary>
        /// Function To test for Alphabets. with Apostrophe s('s)and Blank Char
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>
        /// <Author>Dhrityman Mukherjee</Author>
        /// <Date>05-DEC-2007</Date>
        public bool IsAlphaWithApostropheAndBlank(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z' ]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaWithApostropheAndBlank Method

        #region IsAlphaNumeric Method
       
        /// <summary>
        /// Function to Check for AlphaNumeric.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        public bool IsAlphaNumeric(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumeric Method

        #region IsAlphaNumericWithBlankChar Method

        /// <summary>
        /// Function to Check for Alpha Numeric with Blank Char.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        public bool IsAlphaNumericWithBlankChar(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithBlankChar Method

        #region IsAlphaNumericWithApostrophe Method

        /// <summary>
        /// Function to Check for Alpha Numeric with Apostrophe.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>    
        public bool IsAlphaNumericWithApostrophe(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9']");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithApostrophe Method

        #region IsAlphaNumericWithApostropheBlankChar Method

        /// <summary>
        /// Function to Check for Alpha Numeric with Apostrophe and Blank Char.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>      
        public bool IsAlphaNumericWithApostropheBlankChar(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9' ]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsAlphaNumericWithApostropheBlankChar Method

        #region IsValidFileFolderPathSyntex Method
        /// <summary>
        ///  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.).
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>   
        public bool IsValidFileFolderPathSyntex(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@.$():~/ -]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsValidFileFolderPathSyntex Method

        #region IsSqlInjectionFree Method

        /// <summary>
        /// Function to Check for Sql injection.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        public bool IsSqlInjectionFree(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@.$():',/ -]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsSqlInjectionFree Method

        #region IsValidPassword Method
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        public bool IsValidPassword(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9@&$!#]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
        #endregion IsValidPassword Method

        #region IsValidPasswordFormat Method
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>     
        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

    }
} Posted on Thursday, April 3, 2008 8:11 PM Vini | Back to top


Comments on this post: Validation in CS Page file

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net