August 29, 2013
Verifying Email address
Liondancer’s Question:
I’m trying to create a form that verifies that an email address has been entered. Not necessarily check its validity but basically sakjfsfksldjf@email.com. I was wondering how to go about doing it in PURE JavaScript and no RegEx.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Test File</title>
</head>
<script>
function submitForms() {
email_format = document.getElementById('email_input') // want to return T or F
if (email_format) {
//print email successful
}
else {
//print error message
}
</script>
<body>
<form>
Email:
<input type ="email" id ="email_input" />
</form>
<button type = "button" onclick = "submitForms;"> Submit All!
</button>
</body>
</html>
Here is a previously answered regular expression answer. Its pretty good.
function validateEmail(email) {
var re = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(
".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA
-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
Credits: http://stackoverflow.com/a/46181/295264
However, the only good to verify an email address is by sending a confirmation email and hearing back.