crackon
Programmer
- Jun 17, 2009
- 26
I need a bit of script that checks an email off a form to make sure its valid - cant anyone help
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
function validEmailAddress(email) {
if (typeof(email) != 'string') return false;
var atIndex = email.lastIndexOf("@");
if (atIndex == -1) return false;
var domain = email.substr(atIndex + 1);
var local = email.substr(0, atIndex);
var localLen = local.length;
var domainLen = domain.length;
if (localLen < 1 || localLen > 64) {
// local part length exceeded
return false;
} else if (domainLen < 1 || domainLen > 255) {
// domain part length exceeded
return false;
} else if (local.charAt(0) == '.' || local.charAt(localLen-1) == '.') {
// local part starts or ends with '.'
return false;
} else if (local.match(new RegExp('\\.\\.'))) {
// local part has two consecutive dots
return false;
} else if (!domain.match(new RegExp('^[A-Za-z0-9\\-\\.]+$'))) {
// character not valid in domain part
return false;
} else if (domain.match(new RegExp('\\.\\.'))) {
// domain part has two consecutive dots
return false;
} else if (!local.replace(/\\\\/g, "").match(new RegExp('^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$'))) {
// character not valid in local part unless
// local part is quoted
if (!local.replace(/\\\\/g, "").match(new RegExp('^"(\\\\"|[^"])+"$'))) {
return false;
}
} else if (!domain.match(new RegExp('[\\S]+\\.[\\S]+'))) {
// Not part of the RFC, but for our purposes, make sure the domain isn't 'local' (i.e. at least 'x.y', not just 'x')
return false;
}
return true;
}
That is Dan's code, not your.Feherke said:Show us your code.