Last active
March 22, 2017 12:12
-
-
Save kisin/31e10f680c01e1498b36aa3b4bc61669 to your computer and use it in GitHub Desktop.
validate email address using createelement input
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
function isValidEmail(string) { | |
string = string||''; | |
var lastseg = (string.split('@')[1]||'').split('.')[1]||'', | |
input = document.createElement('input'); | |
input.type = 'email'; | |
input.required = true; | |
input.value = string; | |
return !!(string && (input.validity && | |
input.validity.valid) && | |
lastseg.length); | |
} | |
console.log(isValidEmail('')); // -> false | |
console.log(isValidEmail('asda')); // -> false | |
console.log(isValidEmail('asda@gmail')); // -> false | |
console.log(isValidEmail('[email protected]')); // -> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment