Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Created April 10, 2014 14:31
Show Gist options
  • Save kalinchernev/10388605 to your computer and use it in GitHub Desktop.
Save kalinchernev/10388605 to your computer and use it in GitHub Desktop.
Email validators for javascript
/**
* Validates if the input for email field matches requirements and if it's a valid email
* @returns {void}
*/
function validateEmail() {
var inputValue;
// clear previous
jQuery(".reg-validate-email").remove();
inputValue = jQuery(this).val();
if ( inputValue.length > 0 && validateEmailRegEx(inputValue) ) {
jQuery(this).after("<span class='reg-validate-email alert alert-success'>Email is good!<i class='icon-fix icon-ok'></i></span>");
} else {
jQuery(this).after("<span class='reg-validate-email alert alert-error'>Email is not good!<i class='icon-fix icon-exclamation-sign'></i></span>");
}
}
/**
* Helper
* @returns {void}
*/
function validateEmailRegEx(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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment