Reputation: 3198
I have users entering the numbers as
01122449000
The correct format is +5<last 11 digits>
I have tried with
"+5" + tel.match(/\d+/g)[0]
Butists giving incorrect results
Upvotes: 1
Views: 75
Reputation: 3198
function getFormattedPhone(a) {
if (a) return (a = a.match(/\d+/g)) ? "'+2" + a.join("").substr(-11) : ""
};
This worked for me.
Upvotes: 0
Reputation: 520978
You could try stripping whitespace then retaining just the first digit and final 11 digits:
var phone = "5 011 223 39000 01122449000";
var output = phone.replace(/\s+/g, "").replace(/^(\d)\d*(\d{11})$/, "+$1$2");
console.log(phone + "\n" + output);
if (/^\+5\d{11}$/.test(output)) {
console.log("valid phone number");
}
else {
console.log("invalid phone number");
}
Upvotes: 2