using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace CB.Common { public class Validator { /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(object expression) { if (expression != null) return IsNumeric(expression.ToString()); return false; } /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(string expression) { if (expression != null) { string str = expression; 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; } /// /// 是否为Double类型 /// /// /// public static bool IsDouble(object expression) { if (expression != null) return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); return false; } /// /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型 /// /// 要确认的字符串数组 /// 是则返加true 不是则返回 false 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; } } }