Skip to content

Instantly share code, notes, and snippets.

@alanbsmith
Created January 15, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanbsmith/a40594f2416ea1f104307642d63f7999 to your computer and use it in GitHub Desktop.
Save alanbsmith/a40594f2416ea1f104307642d63f7999 to your computer and use it in GitHub Desktop.
// lib/validatePhone.js
...
const validatePhone = (phoneNumber) => {
// validate there are no characters
if (/[a-zA-Z]/.test(phoneNumber)) {
return false;
}
// validate there are enough numbers
const cleanNumber = phoneNumber.replace(/\s|\D/g, "");
// validate country code
if (cleanNumber[0] === "1") {
return cleanNumber.length === 11;
}
return cleanNumber.length === 10;
};
export default validatePhone;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment