using System.Web.Security;
using System.Text;
using System.Text.RegularExpressions;
using System;
namespace Application.Common
{
public static class ValidityUtil
{
/// <summary>
/// 是否是数字。
/// </summary>
/// <param name="o">object</param>
/// <returns>true 是,false 不是</returns>
public static bool IsNumeric(object o)
{
if (IsNullOrDBNull(o) || IsNullOrDBNull(o))
{
return false;
}
Regex reg = new Regex("^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$");
return reg.IsMatch(o.ToString());
}
/// <summary>
/// 是否为-或数字
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static bool IsNegativeNumeric(object o)
{
if (IsNullOrDBNull(o) || IsNullOrDBNull(o))
{
return false;
}
Regex reg = new Regex("^[0-9]+[-]?[0-9]+$");
return reg.IsMatch(o.ToString());
}
/// <summary>
/// 是否是int类型数字。
/// </summary>
/// <param name="o">object</param>
/// <returns>true 是,false 不是</returns>
public static bool IsInt(object o)
{
bool t = true;
if (IsNullOrDBNull(o) || IsNullOrDBNull(o))
{
t= false;
}
try
{
int i = int.Parse(o.ToString());
}
catch (Exception)
{
t = false;
}
return t;
}
/// <summary>
/// 是否是字符串和数字。
/// </summary>
/// <param name="o">object</param>
/// <returns>true 是,false 不是</returns>
public static bool IsStringAndNumeric(object o)
{
if (IsNullOrDBNull(o) || IsNullOrDBNull(o))
{
return false;
}
Regex reg = new Regex("^[A-Za-z0-9]+$");
return reg.IsMatch(o.ToString());
}
/// <summary>
/// 测试对象是否为空的字符串
/// </summary>
/// <param name="o">测试对象</param>
/// <returns>如果对象为空,或者对象对应的字符串为空,则返回true</returns>
public static bool IsNullOrEmpty(Object o)
{
if (o != null)
{
return String.IsNullOrEmpty(o.ToString().Trim());
}
else
{
return true;
}
}
/// <summary>
/// 判断数据库的字段是否为空
/// </summary>
/// <param name="o">数据库字段</param>
/// <returns>为空返回true</returns>
public static bool IsNullOrDBNull(Object o)
{
return (Object.Equals(o, null) || Object.Equals(o, DBNull.Value)
|| String.IsNullOrEmpty(o.ToString().Trim()));
}
/// <summary>
/// 验证Email是否正确
/// </summary>
/// <param name="o">字符串</param>
/// <returns>true 是,false 不是</returns>
public static bool IsEmail(string o)
{
if (string.IsNullOrEmpty(o.Trim()))
{
return false;
}
return Regex.IsMatch(o, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
}
/// <summary>
/// 手机号是否正确
/// </summary>
/// <param name="o">字符串</param>
/// <returns>true 是,false 不是</returns>
public static bool IsMobile(string o)
{
if (string.IsNullOrEmpty(o.Trim()))
{
return false;
}
return Regex.IsMatch(o, @"^[1][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$");
}
/// <summary>
/// 是否为电话号码
/// </summary>
/// <param name="o">字符串</param>
/// <returns>true 是,false 不是</returns>
public static bool IsTel(string o)
{
if (string.IsNullOrEmpty(o.Trim()))
{
return false;
}
return Regex.IsMatch(o, @"^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$");
}
/// <summary>
/// 是否为身份证
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static bool IsIdentityCard(string o)
{
if (string.IsNullOrEmpty(o.Trim()))
{
return false;
}
return Regex.IsMatch(o, @"^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$");
}
/// <summary>
/// 是否为邮编
/// </summary>
/// <param name="o">字符串</param>
/// <returns>true 是,false 不是</returns>
public static bool IsPostCode(string o)
{
if (string.IsNullOrEmpty(o.Trim()))
{
return false;
}
return Regex.IsMatch(o, @"^\d{5}$");
}
/// <summary>
/// 验证字符串为日期时间格式
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static bool IsDatetime(string strValue)
{
string strReg = @"([1-2][0-9][0-9][0-9])-(0*[1-9]|1[0-2])-(0*[1-9]|[12][0-9]|3[01])\ (0*[0-9]|1[0-9]|2[0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";
if (strValue == "")
{
return false;
}
else
{
Regex re = new Regex(strReg);
MatchCollection mc = re.Matches(strValue);
if (mc.Count == 1)
foreach (Match m in mc)
{
if (m.Value == strValue)
return true;
}
}
return false;
}
/// <summary>
/// 注册用户名限制(中文、数字、字母)
/// </summary>
/// <param name="o" />字符串
/// <returns>true 是,false 不是</returns>
public static bool CheckRegName(string uName)
{
if (string.IsNullOrEmpty(uName))
{
return false;
}
return Regex.IsMatch(uName, @"^[A-Za-z0-9_(\u4e00-\u9fa5)]+$");
}
/// <summary>
/// 字符串长度英文为1,中文为2
/// </summary>
/// <param name="str" />
/// <returns></returns>
public int trueLength(string str)
{
// str 字符串
// return 字符串的字节长度
int lenTotal = 0;
int n = str.Length;
string strWord = "";
int asc;
for (int i = 0; i < n; i++)
{
strWord = str.Substring(i, 1);
asc = Convert.ToChar(strWord);
if (asc < 0 || asc > 127)
lenTotal = lenTotal + 2;
else
lenTotal = lenTotal + 1;
}
return lenTotal;
}
}
}