Created
June 11, 2019 15:23
-
-
Save mattlockyer/c0f7822d52188bfecffc8290d4cad77e to your computer and use it in GitHub Desktop.
Validate input as email or phone, with errors
This file contains hidden or 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
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 { | |
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