使用正则表达式判断字符串
后台代码:
/// <summary>
///ShowMatch 的摘要说明
/// </summary>
public class ShowMatch
{
public ShowMatch()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 验证字符串只含字母
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsLettersString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^[A-Za-z]+$");
}
/// <summary>
/// 验证字符串只含汉字
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsChineseString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^[\u4e00-\u9fa5]+$");
}
/// <summary>
/// 验证字符串只含字母和汉字
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsValidString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^[A-Za-z\u4e00-\u9fa5]+$");
}
/// <summary>
/// 验证字符串是正确的电话号码形式(包括手机)
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsPhoneString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)");
}
/// <summary>
/// 验证字符串是正确的传真号码形式
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsByValueString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
}
/// <summary>
/// 验证字符串是正确的电子邮箱格式
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool IsMailString(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^([\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="strIn"></param>
/// <returns></returns>
public bool isNumberstring(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^[-]?\d+[.]?\d*$");
}
/// <summary>
/// 验证字符串是正确的身份证号码
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public bool isIDCARD(string strIn)
{
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
}
}
前台调用:
ShowMatch showmatch = new ShowMatch();
string str="abc";
if (showmatch.IsLettersString(str))
{
Response.Write("true,只含字母");
}
else
{
Response.Write("false");
}