-
-
Save alanbsmith/a40594f2416ea1f104307642d63f7999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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