/// <summary> /// 验证正整数 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string zhengshu(String str) { string flag = string.Empty;
Regex r = new Regex(@"^d+$"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "只能输入0和正整数!"; }
return flag; }
/// <summary> /// 验证小数 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string xiaoshu(String str) { string flag = string.Empty;
Regex r = new Regex(@"^0$|^(-)?((0)?.d+)$|^(-)?([1-9]d*(.d+)?)$"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "只能整数和小数!"; }
return flag; }
#region 截取固定长度的字符串 /// <summary> /// 处理字符串长度 如果长度大于输入长度截取 /// </summary> /// <param name="info"></param> /// <param name="length"></param> /// <returns></returns> public static string getSafeStr2(string info, int length = 15) { if (info == string.Empty) return "";
string retInfo = info.Replace("'", "''");
if (info.Length > length) { retInfo = info.Substring(0, length)+"..."; } return retInfo; } #endregion
/// <summary> /// 验证日期格式 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string riqi(String str) { string flag = string.Empty;
Regex r = new Regex(@"^[1-2]{1}d{3}[0-1]{1}d{1}[0-3]{1}d{1}$"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "日期格式有误(yyyymmdd)!"; }
return flag; }
/// <summary> /// 邮箱格式 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string EmailRegex(String str) { string flag = string.Empty;
Regex r = new Regex(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "邮箱格式有误(yyyymmdd)!"; }
return flag; }
/// <summary> /// 电话验证 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string PhoneRegex(String str) { string flag = string.Empty;
Regex r = new Regex(@"^((d{11})|^((d{7,8})|(d{4}|d{3})-(d{7,8})|(d{4}|d{3})-(d{7,8})-(d{4}|d{3}|d{2}|d{1})|(d{7,8})-(d{4}|d{3}|d{2}|d{1}))$)"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "电话格式有误(yyyymmdd)!"; }
return flag; }
/// <summary> /// 四位数字 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string SZRegex(String str) { string flag = string.Empty;
Regex r = new Regex(@"^d{4}$"); if (r.IsMatch(str)) { flag = "OK"; } else { flag = "数字格式有误(yyyymmdd)!"; }
return flag; }