Skip to content

Regular expression for digits and decimals

s kumar

Asked Dec 6, 2007, 7:35 PM EST1 answer
Share
LinkedInX

hello  frndz,

i  need a regular expression to accept only digits or can  contain decimals. for example,

it  should  accept 10  or  10.00  or 50.55 like  that

thanks in  advance

 kumar

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hai Friend this might help you.

    Regular Expression


     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);
        }


     public bool IsWholeNumber(String strNumber)
        {
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strNumber);
        }

     

    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);
        }


     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);
        }


    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);
        }


      public static bool IsAlphaNumeric(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsAlphaNumericWithSpace(String strToCheck)
        {
            Regex objAlphaNumericSpacePattern = new Regex(@"^[a-zA-Z0-9\s]*$");
            return !objAlphaNumericSpacePattern.IsMatch(strToCheck);
        }

        public static bool IsNumericWithDot(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[0-9.]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsNumeric(String strToCheck)
        {
            Regex objNumericPattern = new Regex(@"^[0-9]*$");
            return !objNumericPattern.IsMatch(strToCheck);
        }
        public static bool IsAlphaNumericWithDot(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9.]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsAlphaNumericWithDotPeriodSpace(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
     
        public static bool IsAlphaNumericWithDotPeriodSpaceHyphen(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsAlphaNumericWithDotPeriodSpaceHyphenSlash(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9\s.,-/]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsAlphaNumericWithSlash(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^[a-zA-Z0-9/]*$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsAmount(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex(@"^(\d{1,12}|(\d{1,12}\.{1}\d{1,3}){1})$");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

          public static bool IsAmountBECost(String strToCheck)
        {
            Regex objAmountPattern = new Regex(@"^(\d{1,8}|(\d{1,8}\.{1}\d{1,3}){1})$");
            return !objAmountPattern.IsMatch(strToCheck);
        }

      

        public static bool IsValidMail(String strToCheck)
        {
            Regex objEmailPattern = new Regex(@"^[a-z0-9A-Z_\+-.]+(\.[a-z0-9A-Z_\+-.]+)*@[a-z0-9A-Z-]+(\.[a-z0-9A-Z-]+)*\.([a-zA-Z]{2,4})$");
            return !objEmailPattern.IsMatch(strToCheck);
        }

    0