2019-2-12 正则表达式验证手机号

只能输入汉字!!!
1
2
3
4
5
6
7
8
9
10
11
12
要引用命名空间
using System.Text.RegularExpressions;
 
        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
                Regex rg = new Regex("^[\u4e00-\u9fa5]$");  //正则表达式
                if (!rg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b'//'\b'是退格键
                {
                     
                    e.Handled = true;
                }
        }

  


  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  //写在keydown事件下
            TextBox txt = sender as TextBox;
 
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) )//自己改建
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
 
//写在textchange事件下
 
            TextBox textBox = sender as TextBox;
            TextChange[] change = new TextChange[e.Changes.Count];
            e.Changes.CopyTo(change, 0);
 
            int offset = change[0].Offset;
            if (change[0].AddedLength > 0)
            {
                double num = 0;
                if (!Double.TryParse(textBox.Text, out num))
                {
                    textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
                    textBox.Select(offset, 0);
                }
            }

  

1
2
3
4
5
6
7
8
9
10
11
12
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//只允许输入0~9中间的数字,你可以修改允许输入的字符的e.keychar值达到你的需求
。 if (e.KeyChar >= '0' && e.KeyChar<='9' )
{
e.Handled = falsereturn;
 }
else
 {
 e.Handled = true;
}
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//限制输入不能为中文和全角
        private void zhbh_KeyPress(object sender, KeyPressEventArgs e)
        {
            int chfrom = Convert .ToInt32("4e00", 16);    //范围(0x4e00~0x9fa5)转换成int(chfrom~chend)
            int chend = Convert .ToInt32("9fa5", 16);
            if (e.KeyChar >= (Char )chfrom && e.KeyChar <= (Char)chend)
            {
                e.Handled = true;
            }
            if (e.KeyChar >= (Char )65281 & (int)e.KeyChar <= ( Char)65374)
            {
                e.Handled = true;
            }
        }
  
//*******以下方法需在文本框的KeyPress方法下调用
        //限制输入只能为数字
        private void wz_qsh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char .IsNumber(e.KeyChar)) && e.KeyChar != (Char)8)
            {
                e.Handled = true;
            }
        }
 /**
         * 限制只能输入数字和小数点
         * */
        public void validationOnlyFloat(KeyPressEventArgs e, TextBox t)
        {
            if (((int )e.KeyChar < 48 || (int)e.KeyChar > 57) && ( int)e.KeyChar != 8 && (int )e.KeyChar != 46)
                e.Handled = true;
            //小数点的处理。
            if ((int )e.KeyChar == 46)                           //小数点
            {
                if (t.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false , b2 = false;
                    b1 = float.TryParse(t.Text, out oldf);
                    b2 = float.TryParse(t.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false )
                    {
                        if (b1 == true )
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

  

验证电话号码的主要代码如下:
public bool IsTelephone(stringstr_telephone)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_telephone,@"^(\d{3,4}-)?\d{6,8}$");
}
验证手机号码的主要代码如下:
public bool IsHandset(string str_handset)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_handset,@"^[1]+[3,5]+\d{9}");
}
验证身份证号的主要代码如下:
public bool IsIDcard(stringstr_idcard)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_idcard,@"(^\d{18}$)|(^\d{15}$)");
}
验证输入为数字的主要代码如下:
public bool IsNumber(stringstr_number)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_number,@"^[0-9]*$");
}
验证邮编的主要代码如下:
public boolIsPostalcode(string str_postalcode)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_postalcode,@"^\d{6}$");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//检测手机号码是否合法
private bool CheckPhoneIsAble(string input)
{
if(input.Length<11)
{
return false;
}
//电信手机号码正则
string dianxin = @"^1[3578][01379]\d{8}$";
Regex regexDX = new Regex(dianxin);
//联通手机号码正则
string liantong = @"^1[34578][01256]\d{8}";
Regex regexLT = new Regex(dianxin);
//移动手机号码正则
string yidong = @"^(1[012345678]\d{8}|1[345678][012356789]\d{8})$";
Regex regexYD = new Regex(dianxin);
if (regexDX.IsMatch(input) || regexLT.IsMatch(input) || regexYD.IsMatch(input))
{
return true;
}
else{
return false;
}
}
---------------------

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
用textbox的keypress事件,然后判断
if((e.keychar<'0'&&e.keychar!=8)||e.keychar>'9')
e.handled=true;
这是只能输入0-9之间的数字的,并且能接受回车键
 
 
C#用法:
textbox文本框的ID:txt_value
汉字: /^[\u4e00-\u9fa5]+$/。
using System.Text.RegularExpressions; //引用命名空间
string Reg_value= @"/^[\u4e00-\u9fa5]+$/";
//只能输入汉文
if (!Regex.IsMatch(txt_value.Text.Trim(), Reg_value))
{
txt_value.Focus();
return ;
}
//这是实现只能输入数字0-9,还有字母A-Z,a-z的
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar <= '9' && e.KeyChar >= '0' || e.KeyChar <= 'Z' && e.KeyChar >= 'A' || e.KeyChar <= 'z' && e.KeyChar >= 'a')
{
e.Handled = false ;
}
else
{
e.Handled = true ;
}
}

  

 

验证邮箱及常用的正则表达式:
       
1
2
3
4
5
6
7
8
9
10
11
12
using System.Text.RegularExpressions;
 
        Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");  
 
        if (r.IsMatch(jieta@sina.com))
        {
            MessageBox.Show("This is a true email");
        }
        else
        {
            MessageBox.Show("This is not a email");
        }

 

1
2
3
4
5
6
7
8
if (this.txtEmail.Text.ToString().Trim() != "")
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(this.txtEmail.Text.ToString().Trim(), @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
                {
                    Msg.InfoBox("请输入正确的邮箱格式!");
                    return false;
                }
            }

  

1
2
3
4
public bool IsEmail(string value)
{
    return System.Text.RegularExpressions.Regex.IsMatch(value, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}

  


       1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A-Z][a-z]_[0-9]"组成,并且第一个字必须为字母6~16位;

  2.验证电话号码:("^(\d{3.4}-)\d{7,8}$")正确格式:xxx/xxxx-xxxxxxx/xxxxxxxx;

  3.验证身份证号(15位或18位数字):("^\d{15}|\d{18}$");

  4.验证Email地址:("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");

  5.只能输入由数字和26个英文字母组成的字符串:("^[A-Za-z0-9]+$") ;

  6.整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$

  7.只能输入数字:"^[0-9]*$"。

  8.只能输入n位的数字:"^\d{n}$"。

  9.只能输入至少n位的数字:"^\d{n,}$"。

  10.只能输入m~n位的数字:。"^\d{m,n}$"

  11.只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。

  12.只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。

  13.只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。

  14.只能输入非零的正整数:"^\+?[1-9][0-9]*$"。

  15.只能输入非零的负整数:"^\-[1-9][]0-9"*$。

  16.只能输入长度为3的字符:"^.{3}$"。

  17.只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。

  18.只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。

  19.只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。

  20.验证是否含有^%&',;=?$\"等字符:"[^%&',;=?$\x22]+"。

  21.只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"

  22.验证URL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。

  23.验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。

  24.验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。

posted @ 2019-08-09 14:32  lywJee  阅读(325)  评论(0编辑  收藏  举报