600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > C# winform 常用正则验证

C# winform 常用正则验证

时间:2019-04-03 03:36:36

相关推荐

C# winform 常用正则验证

C#常用的正则验证

一:网络应用验证

①:验证E_Mail格式

public bool IsEmail(string str_Email){return System.Text.RegularExpressions.Regex.IsMatch(str_Email,//使用正则表达式判断是否匹配@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");}

进行判断

if (!IsEmail(textBox1.Text))//验证Email格式是否正确{ MessageBox.Show("Email格式错误!"); }//弹出消息对话框else { MessageBox.Show("Email格式正确!"); }//弹出消息对话框

②:验证IP是否正确

public bool IPCheck(string IP){string num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";//创建正则表达式字符串return Regex.IsMatch(IP,//使用正则表达式判断是否匹配("^" + num + "\\." + num + "\\." + num + "\\." + num + "$"));}

进行判断

if (IPCheck(textBox1.Text))//验证IP是否正确{MessageBox.Show("输入IP正确");//弹出消息对话框}else { MessageBox.Show("输入IP不正确"); }//弹出消息对话框

③:验证URL

public bool IsUrl(string str_url){return System.Text.RegularExpressions.Regex.IsMatch(str_url,//使用正则表达式判断是否匹配@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");}

进行判断

if (!IsUrl(textBox1.Text))//验证网址格式是否正确{ MessageBox.Show("网址格式不正确!!!"); }//弹出消息对话框else { MessageBox.Show("网址格式正确!!!!!"); }//弹出消息对话框

二:常用的字符串验证

①:验证是否输入字母

public bool IsLetter(string str_Letter){return System.Text.RegularExpressions.Regex.//使用正则表达式判断是否匹配IsMatch(str_Letter, @"^[A-Za-z]+$");}

进行验证

if (!IsLetter(textBox1.Text.Trim()))//验证字符串是否为大小写字母组成{ MessageBox.Show("输入的不是字母!!!", "提示"); }//弹出消息对话框else { MessageBox.Show("输入的是字母!!!!!", "提示"); }//弹出消息对话框

②:验证字符串是否为汉字

public bool IsChinese(string str_chinese){return System.Text.RegularExpressions.Regex.//使用正则表达式判断是否匹配IsMatch(str_chinese, @"^[\u4e00-\u9fa5],{0,}$");}

进行验证

if (!IsChinese(textBox1.Text.Trim()))//验证字符串是否为汉字{ MessageBox.Show("输入的不是中文!!!", "提示"); }//弹出消息对话框else { MessageBox.Show("输入正确!!!!!", "提示"); }//弹出消息对话框

三:常用数字验证技巧

①:验证邮政编码

public bool IsPostalcode(string str_postalcode){return Regex.IsMatch(str_postalcode, @"^\d{6}$");}

进行验证

if (!IsPostalcode(textBox1.Text))//验证邮编格式是否正确{ MessageBox.Show("邮政编号不正确!!!"); }//弹出消息对话框else { MessageBox.Show("邮政编号正确!!!!!"); }//弹出消息对话框

②:验证手机号是否正确

public bool IsHandset(string str_handset){return System.Text.RegularExpressions.Regex.使用正则表达式判断是否匹配IsMatch(str_handset, @"^[1]+[3,8,5]+\d{9}$");}

验证

if (!IsHandset(textBox1.Text))//验证手机号是否正确{ MessageBox.Show("手机号不正确!!!"); }//弹出消息对话框else { MessageBox.Show("手机号正确!!!!!"); }//弹出消息对话框

③:验证身份证是否有效

#region 验证身份证是否有效/// <summary>/// 验证身份证是否有效/// </summary>/// <param name="Id"></param>/// <returns></returns>public static bool IsIDCard(string Id){if (Id.Length == 18){bool check = IsIDCard18(Id);return check;}else if (Id.Length == 15){bool check = IsIDCard15(Id);return check;}else{return false;}}public static bool IsIDCard18(string Id){long n = 0;if (long.TryParse(Id.Remove(17), out n) == false || n<Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false){return false;//数字验证}string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";if (address.IndexOf(Id.Remove(2)) == -1){return false;//省份验证}string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");DateTime time = new DateTime();if (DateTime.TryParse(birth, out time) == false){return false;//生日验证}string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');char[] Ai = Id.Remove(17).ToCharArray();int sum = 0;for (int i = 0; i< 17; i++){sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());}int y = -1;Math.DivRem(sum, 11, out y);if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower()){return false;//校验码验证}return true;//符合GB11643-1999标准}public static bool IsIDCard15(string Id){long n = 0;if (long.TryParse(Id, out n) == false || n<Math.Pow(10, 14)){return false;//数字验证}string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";if (address.IndexOf(Id.Remove(2)) == -1){return false;//省份验证}string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");DateTime time = new DateTime();if (DateTime.TryParse(birth, out time) == false){ return false;//生日验证}return true;//符合15位身份证标准}#endregion

验证

if (!IsIDCard(textBox1.Text.Trim()))//验证身份证号是否正确{ MessageBox.Show("身份证号不正确!!!"); }//弹出消息对话框else { MessageBox.Show("身份证号正确!!!!!"); }//弹出消息对话框

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。