001
001

Reputation: 65205

Validate a phone number?

What are the conditions to validate a phone number?

Well firstly it has to be a numerical value, which 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?

Upvotes: 0

Views: 11816

Answers (7)

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

I would use the 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

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

Upvotes: 0

Shailendra Mishra
Shailendra Mishra

Reputation: 71

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;
    }
}

Upvotes: 0

Guyver
Guyver

Reputation: 165

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}$/);

Upvotes: 0

Zzz
Zzz

Reputation: 220

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...

Upvotes: 0

Pabuc
Pabuc

Reputation: 5638

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

Upvotes: 0

Alex Mendez
Alex Mendez

Reputation: 5150

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

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89232

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.

Upvotes: 8

Related Questions