zoukankan      html  css  js  c++  java
  • 验证数据对象类型

     /// <summary>
            /// 判断对象是否为Int32类型的数字
            /// </summary>
            /// <param name="Expression"></param>
            /// <returns></returns>
            public static bool IsNumeric(object Expression)
            {
                if (Expression != null)
                {
                    string str = Expression.ToString();
                    if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                    {
                        if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        {
                            return true;
                        }
                    }
                }
                return false;

            }


            public static bool IsDouble(object Expression)
            {
                if (Expression != null)
                {
                    return Regex.IsMatch(Expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
                }
                return false;
            }

            /// <summary>
            /// string型转换为bool型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的bool类型结果</returns>
            public static bool StrToBool(object Expression, bool defValue)
            {
                if (Expression != null)
                {
                    if (string.Compare(Expression.ToString(), "true", true) == 0)
                    {
                        return true;
                    }
                    else if (string.Compare(Expression.ToString(), "false", true) == 0)
                    {
                        return false;
                    }
                }
                return defValue;
            }

            /// <summary>
            /// 将对象转换为Int32类型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static int StrToInt(object Expression, int defValue)
            {

                if (Expression != null)
                {
                    string str = Expression.ToString();
                    if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
                    {
                        if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        {
                            return Convert.ToInt32(str);
                        }
                    }
                }
                return defValue;
            }

            /// <summary>
            /// string型转换为float型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static float StrToFloat(object strValue, float defValue)
            {
                if ((strValue == null) || (strValue.ToString().Length > 10))
                {
                    return defValue;
                }

                float intValue = defValue;
                if (strValue != null)
                {
                    bool IsFloat = Regex.IsMatch(strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                    if (IsFloat)
                    {
                        intValue = Convert.ToSingle(strValue);
                    }
                }
                return intValue;
            }


            /// <summary>
            /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
            /// </summary>
            /// <param name="strNumber">要确认的字符串数组</param>
            /// <returns>是则返加true 不是则返回 false</returns>
            public static bool IsNumericArray(string[] strNumber)
            {
                if (strNumber == null)
                {
                    return false;
                }
                if (strNumber.Length < 1)
                {
                    return false;
                }
                foreach (string id in strNumber)
                {
                    if (!IsNumeric(id))
                    {
                        return false;
                    }
                }
                return true;

            }

  • 相关阅读:
    阿里云ECS安装sqlserver,本地无法连接问题排查思路
    1433端口无法连接(sql server 数据库无法访问问题)解决思路
    开源框架 电商参考系统
    版本控制工具 Git 只下载开源项目的某个文件夹
    VUE 在idea中的运行项目
    开源框架 Java Guns 03 数据库替换为sqlite
    SQL Server 用ip地址登录 127.0.0.1
    开源框架 UI框架
    电商 电商系统汇总
    电商 平台汇总
  • 原文地址:https://www.cnblogs.com/hqbird/p/1294298.html
Copyright © 2011-2022 走看看