ASP.NET正规表达式验证数字、字母、汉字、电话、电子邮件、身份证判断、数组随机排序
using System.Text.RegularExpressions;//正则表达式命名空间
#region 判断文本是否全是字母组合
/// <summary>
/// 名称:IsAllChar
/// 判断文本是否全是字母组合
/// </summary>
/// <param name="text">需判断的文本或是字符串</param>
/// <returns>返回true代表全是字母组合</returns>
public static bool IsAllChar(string text)
{
foreach(char tempchar in text.ToCharArray())
{
if (!char.IsLetter(tempchar))
{
return false;
}
}
return true;
}
#endregion
#region 判断文本是否纯数字组合
/// <summary>
/// 名称:IsAllNumber
/// 判断文本是否全数字组成函数1(推荐使用)
/// </summary>
/// <param name="text">需判断的文本或字符串</param>
/// <returns>返回true代表纯数字,否则为非纯数字</returns>
public static bool IsAllNumber(string text)
{
foreach(char tempchar in text.ToCharArray())
{
if (tempchar == '.')
{
return false;
}
}
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(text) && !objTwoDotPattern.IsMatch(text) && !objTwoMinusPattern.IsMatch(text) && objNumberPattern.IsMatch(text);
}
#endregion
#region 判断文本是否是数字(包括小数位的判断)
/// <summary>
/// 名称:IsNumber
/// 判断是否数字函数1(推荐使用)
/// </summary>
/// <param name="text">需判断的文本或字符串</param>
/// <returns>返回true代表数字,否则为非数字</returns>
public static bool IsNumber(string text)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(text) && !objTwoDotPattern.IsMatch(text) && !objTwoMinusPattern.IsMatch(text) && objNumberPattern.IsMatch(text);
}
#endregion
#region 全汉字判断
/// <summary>
/// 名称:IsAllChina
/// 判断是否全是汉字组合
/// </summary>
/// <param name="text">需判断的文本</param>
/// <returns>返回True为全是汉字组合</returns>
public static bool IsAllChina(string text)
{
foreach (char c in text.ToCharArray())
{
if (Regex.IsMatch(c.ToString(), @"^[\u4e00-\u9fa5]+$") == false)
{
return false;
}
}
return true;
}
#endregion
#region 返回文本中的数字部分(适合double与int)
/// <summary>
/// 方法名称:GetDoubleNumber
/// 功能:从数字和字母组合中返回带小数位的数字(移除非数字部分)
/// </summary>
/// <param name="text">所要进行处理的字符串</param>
/// <returns>返回double的数字</returns>
public static double GetDoubleNumber(string text)
{
double result = 0;
if (text != null && text != string.Empty)
{
// 正则表达式剔除非数字字符(不包含小数点.)
text = Regex.Replace(text, @"[^\d.\d]", "");
// 如果是数字,则转换为int类型
if (Regex.IsMatch(text, @"^[+-]?\d*[.]?\d*$"))
{
result = double.Parse(text);
}
}
return result;
}
/// <summary>
/// 方法名称:GetIntNumber
/// 功能:从数字和字母组合中返回整型数字(移除非数字部分)
/// </summary>
/// <param name="text">所要进行处理的字符串</param>
/// <returns>返回整型数字</returns>
public static int GetIntNumber(string text)
{
int result = 0;
if (text != null && text != string.Empty)
{
// 正则表达式剔除非数字字符(不包含小数点.)
text = Regex.Replace(text, @"[^\d.\d]", "");
// 如果是数字,则转换为int类型
if (Regex.IsMatch(text, @"^[+-]?\d*[.]?\d*$"))
{
result = int.Parse(text);
}
}
return result;
}
/// <summary>
/// 文本框限定输入数字
/// </summary>
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//检测是否已经输入了小数点
bool IsContainsDot = this.textBox1.Text.Contains(".");
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
else if (IsContainsDot && (e.KeyChar == 46)) //如果输入了小数点,并且再次输入
{
e.Handled = true;
}
}
#endregion
using System.Text.RegularExpressions;//正则表达式命名空间
#region 电话号码判断
/// <summary>
/// 名称:CheckPhoneNumber
/// 方法:判断是否合法的电话号码
/// </summary>
/// <param name="number">所要判断的电话号码或手机号码</param>
/// <returns>true,false</returns>
public static bool CheckPhoneNumber(string number)
{
if (System.Text.RegularExpressions.Regex.IsMatch(number, @"^(?:0(?:10|2[0-57-9]|[3-9]\d{2})[-—]?)\d{7,8}$"))
{
return true;
}
else if (System.Text.RegularExpressions.Regex.IsMatch(number, "^1\\d{10}$"))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 电子邮件判断
/// <summary>
/// 名称:CheckEmail
/// 功能:判断是否正确的电子邮件
/// </summary>
/// <param name="inputEmail">所要判断的电子邮件号</param>
/// <returns>true,false</returns>
public static bool CheckEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 身份证内信息提取
/// <summary>
/// 名称:CheckPassPortChina
/// 功能:检查中国公民身份证是否正确
/// </summary>
/// <param name="cid">需检查的身份证号码</param>
/// <returns>返回由省市,生日,性别组成的字符串</returns>
private string CheckPassPortChina(string cid)
{
string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
double iSum = 0;
string info = "";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
if (!mc.Success)
{
return "";
}
cid = cid.ToLower();
cid = cid.Replace("x", "a");
if (aCity[int.Parse(cid.Substring(0, 2))] == null)
{
return "非法地区";
}
try
{
DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
}
catch
{
return "非法生日";
}
for (int i = 17; i >= 0; i--)
{
iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
}
if (iSum % 11 != 1)
{
return ("非法证号");
}
return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女"));
}
#endregion
#region 检查身分证的合法性
/// <summary>
/// 名称:CheckCidInfo
/// 功能:检查中国公民身份证号码
/// </summary>
/// <param name="cid">需检查的身份证号</param>
/// <returns>true,false</returns>
private bool CheckCidInfo(string cid)
{
string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
double iSum = 0;
string info = "";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
if (!mc.Success)
{
return false; ;
}
cid = cid.ToLower();
cid = cid.Replace("x", "a");
if (aCity[int.Parse(cid.Substring(0, 2))] == null)
{
return false;
}
try
{
DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
}
catch
{
return false;
}
for (int i = 17; i >= 0; i--)
{
iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
}
if (iSum % 11 != 1)
return (false);
//return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女"));
return true;
}
#endregion
#region 返回倒序的字符串
/// <summary>
/// 名称:Reverse
/// 功能:倒序字符串
/// </summary>
/// <param name="Str">字符串</param>
/// <returns>返回字符串倒序结果</returns>
public static string Reverse(string Str)
{
char[] charArray = Str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
#endregion
/// <summary>
/// 二维数组随机排序(新牌)
/// </summary>
private void btnOrderBy_Click(object sender, EventArgs e)
{
string[] StrArray = new string[81];
this.richTextBox1.Text = "";//清空richtextbox
#region 初始化你的二维数组
string[,] identify = new string[9, 9]{{"教", "律", "医", "导", "音", "美", "游", "旅", "羽"},
{"师", "行", "生", "学", "乐", "术", "戏", "其", "球"},
{"去", "吧", "看", "哦", "人", "个", "恶", "我", "他"},
{"在", "中", "张", "值", "才", "从", "程", "吃", "是"},
{"数", "所", "耍", "说", "的", "大", "对", "到", "飞"},
{"放", "发", "非", "法", "副", "过", "给", "该", "故"},
{"就", "将", "家", "键", "了", "类", "来", "浪", "连"},
{"调", "同", "它", "和", "好", "后", "哈", "并", "帮"},
{"平", "拍", "派", "快", "可", "框", "宇", "要", "敏"}
};
#endregion
#region 二维数组转换为一维
int x = 0;
for (int count = 0; count < identify.GetLength(0); count++)
{
for (int countone = 0; countone < identify.GetLength(1); countone++)
{
StrArray[x] = identify[count, countone].ToString();
x++;
}
}
#endregion
#region 对一维数组进行随机排序
Random rd = new Random();
for (int count = 0; count < StrArray.Length; count++)
{
int tempCount = rd.Next(count, StrArray.Length);
string temp = StrArray[tempCount];
StrArray[tempCount] = StrArray[count];
StrArray[count] = temp;
}
#endregion
#region 用richtextbox输出
x = 0;
for (int count = 0; count < identify.GetLength(0); count++)
{
for (int countone = 0; countone < identify.GetLength(1); countone++)
{//在此也可使其又转化为二维。下面是直接写到richtextbox里了。
//identify[count, countone] = StrArray[x];这行是把转换为二维数组
this.richTextBox1.AppendText(StrArray[x]);
this.richTextBox1.AppendText("\t");
x++;
}
this.richTextBox1.AppendText("\n\n");
}
#endregion
}