手机号码文本框验证 [英] Mobile number textbox validation

查看:175
本文介绍了手机号码文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Text.RegularExpressions;

Regex re = new Regex("^9[0-9]{9}");
if (re.IsMatch(txtMob.Text.Trim()) == false || txtMob.Text.Length > 10)
{
  MessageBox.Show("Invalid  Mobile Number !!");
  txtMob.Focus();
}





我已实施上述代码,并显示任何给定号码的无效手机号码。



我想实现验证,当输入任何字符时会显示不允许字符消息,当超过10位数时显示无效数字消息输入。



请帮帮我!



I have implemented the above code and it displays invalid mobile number for any given number.

I would like to implement validations, which would show a "Characters are not allowed" message when any characters are typed and show an invalid number message when more than 10 digits are entered.

Please help me out!

推荐答案

on TextBox KeyPress事件编写此代码:< br $>


on TextBox KeyPress event write this code :

private void txy_Keypress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & (Keys)e.KeyChar != Keys.Enter)
            {

                e.Handled = true;
            }

            base.OnKeyPress(e);
        }



和TextChanged事件写下这段代码:




and on TextChanged event write this code :

if (txtMob.Text.Length > 10)
{

MessageBox.Show("Invalid Mobile Number !!");

txtMob.Focus();
}





或设置文字Maxlenth proparty set 10





希望这个帮助



or set text Maxlenth proparty set 10


hope this help


我不会通过在TextBox中使用按键来实现。我如何进行验证是接受免费键入的电话号码,然后从输入中删除所有非数字字符,验证相关国家/地区的号码,根据规则将数字重新格式化为字符串国家。



它允许用他们选择的任何格式输入数字,我仍然可以获得我想要的数据而不会让客户感到沮丧。



顺便说一下,你是正则表达式模式要求输入的每个数字都是10位数字,总是以9开头。
I wouldn't do it by using keypresses in a TextBox. How I do my validations is to accept a free-typed phone number, then go a strip all non-numeric characters from the input, validate the number for the country in question, the reformat the number as a string according to the rules for that country.

That allows used to type numbers in in anyn format they chose and I still get the data I want without frustrating the customer.

By the way, youre regex pattern demands that every number typed in be 10 digits long and always start with a 9.


这篇关于手机号码文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆