Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Created June 11, 2019 15:23
Show Gist options
  • Save mattlockyer/c0f7822d52188bfecffc8290d4cad77e to your computer and use it in GitHub Desktop.
Save mattlockyer/c0f7822d52188bfecffc8290d4cad77e to your computer and use it in GitHub Desktop.
Validate input as email or phone, with errors
let contact = 'test', data, error;
const first = contact.substring(0, 1)
if (first === '+' || first === '(' || !isNaN(first)) {
//phone
console.log('testing phone')
const phone = contact.replace(/[+|-|(|)|.| ]/g, "").toLowerCase()
if (phone === "" || isNaN(phone)) {
error = "Please enter a proper phone number.";
} else if (phone.length < 10) {
error = "Your phone number is not long enough. Please include the Area Code.";
} else {
data = phone
}
} else {
//email
console.log('testing email')
const isEmail = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(contact)
if (!isEmail) error = "Please enter a proper email address."
else data = contact
}
console.log(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment