Skip to content

Instantly share code, notes, and snippets.

@richwednesday
Created November 29, 2017 10:39
Show Gist options
  • Save richwednesday/f256f832b1800d7183f92ed912b3c315 to your computer and use it in GitHub Desktop.
Save richwednesday/f256f832b1800d7183f92ed912b3c315 to your computer and use it in GitHub Desktop.
sanitizePhoneNumber(phone, ext, errh) {
errh = errh || function() {};
if(typeof phone !== "string" && !(phone instanceof String)) {
errh("Phone number should be a string, "+(typeof phone)+" passed.");
return false;
}
if (phone.charAt(0) === "+") phone = phone.substring(1);
if (isNaN(Number(phone))) {
errh ("Phone number contains non-numeric characters");
return false;
}
if (phone.length === 11 && phone.indexOf("0") === 0) {
return phone;
}
if (phone.length === 10) return "0" + phone;
if (typeof ext === "string" || ext instanceof String) {
if (phone.length === ext.length+10 && phone.indexOf(ext) === 0) {
return "0"+phone.substring(ext.length);
} else {
errh("Invalid phone number");
return false;
}
} else {
errh("Invalid phone number");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment