Solution Timeline

All versions (edits) of solutions to Super Robust Phone Number Validation in Javascript appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

You can't use PhoneFormat.js? is really solid and built using Google's libphonenumber library.

I am unsure as what exactly you are looking for, but I belive these functions below might work. All you have to do is to run the string through the function. The first function numbersOnly will take any string and remove all non-number characters. The second function will do the same but also will only accept the first 15 characters (because the E.164 recommendation for phone numbers only allows 15 characters). The third function does what the second function does but also adds the + infront of the numbers.

function numbersOnly(string) { 
    return(string.replace(/[^0-9]/g, ''));
}

function numberLength(string) {
    return(string.replace(/[^0-9]/g, '').substring(0,15))
}

function numberPlus(string) {
    return('+'+string.replace(/[^0-9]/g, '').substring(0,15))
}

I am unsure as what exactly you are looking for, but I belive these functions below might work. All you have to do is to run the string through the function. The first function numbersOnly will take any string and remove all non-number characters. The second function will do the same but also will only accept the first 15 characters (because the E.164 recommendation for phone numbers only allows 15 characters). The third function does what the second function does but also adds the + infront of the numbers.

function numbersOnly(string) { 
    return(string.replace(/[^0-9]/g, ''));
}

function numberLength(string) {
    return(string.replace(/[^0-9]/g, '').substring(0,15));
}

function numberPlus(string) {
    return('+'+string.replace(/[^0-9]/g, '').substring(0,15));
}

I am unsure as what exactly you are looking for, but I belive these functions below might work. All you have to do is to run the string through the function. The first function numbersOnly will take any string and remove all non-number characters. The second function will do the same but also will only accept the first 15 characters (because the E.164 recommendation for phone numbers only allows 15 characters). The third function does what the second function does but also adds the + infront of the numbers.

function numbersOnly(string) { 
    return(string.replace(/[^0-9]/g, ''));
}

// 1a234d5-6f7g8(901[]2)3/456 => 1234567890123456

function numberLength(string) {
    return(string.replace(/[^0-9]/g, '').substring(0,15));
}

// 1a234d5-6f7g8(901[]2)3/456 => 123456789012345

function numberPlus(string) {
    return('+'+string.replace(/[^0-9]/g, '').substring(0,15));
}

// 1a234d5-6f7g8(901[]2)3/456 => +123456789012345

I am unsure as what exactly you are looking for, but I belive these functions below might work. All you have to do is to run the string through the function. The first function numbersOnly will take any string and remove all non-number characters. The second function will do the same but also will only accept the first 15 characters (because the E.164 recommendation for phone numbers only allows 15 characters). The third function does what the second function does but also adds the + infront of the numbers.

function numbersOnly(string) { 
    return(string.replace(/[^0-9]/g, ''));
}

// 1a234d5-6f7g8(901[]2)3/456 => 1234567890123456

function numberLength(string) {
    return(string.replace(/[^0-9]/g, '').substring(0,15));
}

// 1a234d5-6f7g8(901[]2)3/456 => 123456789012345

function numberPlus(string) {
    return('+'+string.replace(/[^0-9]/g, '').substring(0,15));
}

// 1a234d5-6f7g8(901[]2)3/456 => +123456789012345

function formatNumberUS(string) {
    var string = string.replace(/[^0-9]/g, '').substring(0,10);
    var first3 = string.substring(0,3);
    var next3 = string.substring(3,6);
    var next4 = string.substring(6,10);
    return("+1("+first3+") "+next3+"-"+next4);
}

//9\[[a0[]]a0+as84dasd,,<>61sdf6as41 => +1(900) 846-1641
Winning solution