C#驗證正數

       //匹配正數
        public static bool IsNumNotMinus(string str)
        {
            bool isMatch = IsMatchRegular(str, @"^[0]*([.]\d+)$");
            if (isMatch == false)
            {
                bool isMatch1 = IsMatchRegular(str, @"^[1-9][0-9]*([.]\d+)?$");
                if (isMatch1 == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }

        }

以上驗證的是大於0的數,如果包括0,  bool isMatch = IsMatchRegular(str, @"^[0]*([.]\d+)$");改爲

  bool isMatch = IsMatchRegular(str, @"^[0]*([.]\d+)?$");就行了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章