开发者

Validate a phone number?

开发者 https://www.devze.com 2023-01-28 21:47 出处:网络
What are the condition to validate a phone 开发者_JS百科number? Well firstly it has to be a numerical value, can include the following numbers

What are the condition to validate a phone 开发者_JS百科number?

Well firstly it has to be a numerical value, can include the following numbers 0123456789 and the plus symbol. +

But what about the following conditions, where do you get this data?

-It has to be in the format [country dial code + area code + phone number]

-It has to be a min and max length

EDIT: also what is the min and max length of a mobile/cell phone number?


It's probably not worth trying to validate a phone number if the phone can be anywhere in the world.

  1. What about extensions?
  2. 1-800-Flowers
  3. Numbers are different depending on where the call originates (inside/outside the country)

The places that I've seen that actually need to verify it (like craigslist) call the number and make you verify the information they give you over the phone.


You have to also validate the characters ()- and have to account for the posiblility of international numbers.


It has to have a fixed length, Maybe you can include the area codes in a combobox and rest of the number in a textbox. On change of combobox, you can set the maxLength of the textbox and change areacode part of your regex. Here is a sample:

/// 3 digits of area code like (333)
string areaCodeRegExp = @"(?<areaCodeGroup>\(\d\d\d\))";
/// xxx-xxxx phone num ex: 333-3333 
string phoneRegExp = @"(?<phoneGroup>\d\d\d\-\d\d\d\d)";


if (System.Text.RegularExpressions.Regex.IsMatch(text, areaCodeRegExp + " " + phoneRegExp))
{
     // this will be valid if phone is (312) 333-4453
}

You can add different values to the combobox for different countries and it will work


You can look here http://regexlib.com/Search.aspx?k=phone+number&c=-1&m=-1&ps=20

I've entered phone number as key words in search box...


Here's one I did for a phone number in JavaScript, shouldn't be too different. But can't Visual Studio do that for you automatically?

 var ph = refp.search(/^[1-9][0-9]{2}-[0-9]{3}-[0-9]{4}$/);


private void txtContactNo_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    if (e.KeyChar == '.'
     && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}


I would use libphonenumbner-csharp library.

If we take a simple phone number:

var util = PhoneNumberUtil.GetInstance();

var number = util.Parse("+1-800-800-6000", "US");

Console.WriteLine(util.IsValidNumber(number));
Console.WriteLine(number.CountryCode.ToString());
Console.WriteLine(util.GetRegionCodeForCountryCode(number.CountryCode));
Console.WriteLine(number.NationalNumber.ToString());
Console.WriteLine(util.Format(number, PhoneNumberFormat.E164));

For instance, if the phone number is not correct like

var number = util.Parse("+1-800-800-60001", "US");

or

var number = util.Parse("+1-800-800-600", "US");

than util.IsValidNumber(number) will false

Source

  • https://github.com/twcclegg/libphonenumber-csharp
  • https://github.com/google/libphonenumber

disclaimer: I have made a little article about this in my personal blog.

0

精彩评论

暂无评论...
验证码 换一张
取 消