3

I want to validate the phone and fax number fields. I want to validate them as phone numbers and fax are given in American format. I have searched but not succeeded. Any one can help how can I validate in following format

Edit 1 (415) xxx xxxx

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
  • Duplicate of http://stackoverflow.com/questions/1141933/validate-phone-number-existence-for-united-states – John Hoven Apr 30 '11 at 13:00
  • Please specify what number formats you think are acceptable, and which formats are not. For example, which of the following are valid for you: "+1 303 433 1321", "1-303-343-1341", "(333) 341-1234", "333.123.1234", "333-123-1234", "3331231234". – Phrogz Apr 30 '11 at 13:01
  • Are you familiar with regular expressions? The question [US Phone Number Verification](http://stackoverflow.com/q/175488/427545) contains a good one. – Lekensteyn Apr 30 '11 at 13:02
  • @benjynito That question is about validating existence of a number, not format. – Phrogz Apr 30 '11 at 13:04
  • If it's just a phone number field, regexs are not needed. All you need is ten digits, and the actual given format by whoever is entering it is irrelevant IMO. EDIT: Except to get rid of everything but the numbers... Oops. – Jared Farrish Apr 30 '11 at 13:07
  • @Phrogz I need in this format `1 (415) xxx xxxx` – Awais Qarni Apr 30 '11 at 13:12
  • Not a duplicate; this question asks [ambiguously] about American phone numbers. The suggested duplicates are about USA phone numbers. "America" refers to a landmass consisting of three continents, not a country. :) – Lightness Races in Orbit Apr 30 '11 at 14:07

5 Answers5

7

The best way is to remove all non-digits and then format it into your preferred format yourself.

var raw_number = str.replace(/[^0-9]/g,'');
var regex1 = /^1?([2-9]..)([2-9]..)(....)$/;
if(!regex1.test(raw_number)) {
    // is invalid...
} else {
    var formatted_number = str.replace(regex1,'1 ($1) $2 $3')
}

That way if they enter 234/555-0123 it will become your preferred format of 1 (234) 555 0123.

Random832
  • 37,415
  • 3
  • 44
  • 63
2

Using regular expressions is fine, I guess, if you are conscienciously limiting your feature to US-formed phone numbers.

However as soon as your code has to deal with international, that method is no longer the most appropriate option. If you have such plans of any kind, and since you are using JavaScript, I suggest that you have a look at Google's libphonenumber.

Eric Redon
  • 1,712
  • 12
  • 21
2

The following Javascript REGEX validates 123-456-7890 or 123.456.7890 or 1234567890 or 123 456 7890 or (123) 456-7890

^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
Billbad
  • 17,651
  • 2
  • 21
  • 17
1

PhoneFormat.com has a javascript library that makes formatting and validating numbers really easy.

AlBeebe
  • 8,101
  • 3
  • 51
  • 65
1

Here is one I use (jQuery) for a field validation onBlur:

http://jsfiddle.net/userdude/Ju72W/

jQuery(document).ready(function($){
    $cf = $('#customfield_10117');
    $cf.blur(function(e){
        phone = $(this).val();
        phone = phone.replace(/[^0-9]/g,'');
        if (phone.length != 10) {
            e.preventDefault();
            if (confirm('Phone number must include area code and prefix.')) {
                setTimeout(function(){$cf.focus()}, 0);
            }
        } else {
            area = phone.substring(0,3);
            prefix = phone.substring(3,6);
            line = phone.substring(6);
            $(this).val('(' + area + ') ' + prefix + '-' + line);
        }
    });
});

It checks if there were 10 numbers submitted, and then if that is true, reformats to (000) 000-0000 format.

EDIT

A function that uses the same technique (with an added country code qualifier).

http://jsfiddle.net/userdude/Ju72W/1/

jQuery(document).ready(function($){
    $cf = $('#customfield_10117');
    $cf.blur(function(e){
        number = phoneCheckAndFormat(this.value, 11);
        if (number === false) {
            alert('Entered phone number is not correct.');
            return;
        }
        $(this).val(number);
    });
});

function phoneCheckAndFormat(phone, digits) {
    phone = phone.replace(/[^0-9]/g,'');
    digits = (digits > 0 ? digits : 10);
    if (phone.length != digits) {
        return false;
    } else {
        code = '';
        if (digits == 11) {
            code = '1 ';
            phone = phone.substring(1);
        }
        area = phone.substring(0,3);
        prefix = phone.substring(3,6);
        line = phone.substring(6);
        return code + '(' + area + ') ' + prefix + '-' + line;
    }
}
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104