C#如何使用正则表达式来校验字段串输入的系列函数
#region 输入字段串校验系列函数
/// <summary>
///获得字符串长度
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int GetStrLength(string str)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(str);
return sarr.Length;
}
/// <summary>
///返回指定长度字符串。
/// </summary>
/// <param name="str">字符串</param>
/// <param name="len">长度</param>
/// <returns>指定长度字符串</returns>
/// <remarks>
/// 在文本编辑框(TextBox控件)中,一个中文汉字和一个英文字母一样都是占一位字节,
/// 但是在SQL Server数据库中,存储一个中文汉字是占用两位字节,这样就可能会导致
/// 在保存文本编辑框的内容时字节被截断,保存出错。所以就需要返回指定长度的当前 ANSI 代码页的编码字符。
/// <code>
/// string strTmp = txtMemo.Text;
/// strTmp=SinoSofts.Common.PublicClass.GetStrEncoding(strTmp,txtMemo.MaxLength);
/// txtMemo.Text=strTmp;
/// </code>
/// </remarks>
public static string GetStrEncoding(string str,int len)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(str);
if(sarr.Length>len && len>0)
return System.Text.Encoding.Default.GetString(sarr,0,len);
else
return str;
}
/// <summary>
/// //判断身份证
/// </summary>
/// <param name="Cert"></param>
/// <returns></returns>
public static bool IsValidCert(string Cert)
{
return Regex.IsMatch(Cert, @"^(\d{15}|\d{17}[\dx])$");
}
//判断邮件
/*public static bool IsValidEmail(string email)
{
return Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}*/
/// <summary>
///判断用户名
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public static bool IsValidUserName(string UserName)
{
return Regex.IsMatch(UserName, @"^([a-z]|[A-Z]){1}\w{0,27}$");
}
/// <summary>
///判断英文名
/// </summary>
/// <param name="EnPersonName"></param>
/// <returns></returns>
public static bool IsValidEnPersonName(string EnPersonName)
{
return Regex.IsMatch(EnPersonName, @"^[a-zA-Z]{1,30}$");
}
/// <summary>
/// 判断密码
/// </summary>
/// <param name="PassWord"></param>
/// <returns></returns>
public static bool IsValidPassWord(string PassWord)
{
return Regex.IsMatch(PassWord, @"^(\w){6,20}$");
}
/// <summary>
/// 判断电话/传真
/// </summary>
/// <param name="Tel"></param>
/// <returns></returns>
public static bool IsValidTel(string Tel)
{
return Regex.IsMatch(Tel, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
}
/// <summary>
/// 判断手机
/// </summary>
/// <param name="Mobil"></param>
/// <returns></returns>
public static bool IsValidMobil(string Mobil)
{
return Regex.IsMatch(Mobil, @"^(\d)+[-]?(\d){6,12}$");
}
/// <summary>
/// 邮政编码
/// </summary>
/// <param name="Zip"></param>
/// <returns></returns>
public static bool IsValidZip(string Zip)
{
return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$");
}
/// <summary>
/// 判断日期
/// </summary>
/// <param name="Date"></param>
/// <returns></returns>
public static bool IsValidDate(string Date)
{
bool bValid=Regex.IsMatch(Date, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$");
return (bValid && Date.CompareTo("1753-01-01")>=0);
}
//判断整型
/*public static bool IsValidInt(string Int)
{
return Regex.IsMatch(Int, @"^[1-9]{1}[0-9]{0,6}$");
}*/
/// <summary>
/// 判断只能输字母
/// </summary>
/// <param name="EnName"></param>
/// <returns></returns>
public static bool IsValidEnName(string EnName)
{
return Regex.IsMatch(EnName, @"[a-zA-Z]");
}
#endregion