(function() {
// define string prototypes for later testing.
var validEmailRegex = /^(([^<>()[\]\\.,;:\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,}))$/;
String.prototype.isEmail = function() {
// returns true if a string is a valid email
return this.match(validEmailRegex);
};
String.prototype.isUKEmail = function() {
// returns true if a string contains .co.uk
return this.toLowerCase().indexOf(".co.uk") === -1 ? false : true;
}
String.prototype.isValidUKEmail = function() {
// returns true if a mail is valid and uk.
return this.isEmail() && this.isUKEmail();
}
})();
// example use
// data set
var data = [
'"Abc\@def"@example.com',
'"Fred Bloggs"@example.com',
'"Joe\\Blow"@example.com',
'"Abc@def"@example.com',
'customer/department=shipping@example.com',
'$A12345@example.com',
'foo@copmpany.co.uk',
'!def!xyz%abc@example.com',
'_somename@example.com'
];
// analyze them all
var count = data.length, isUK;
while (count--) {
if (!data[count].isEmail())
document.write("INVALID: the email <strong>" + data[count] + "</strong> incorrectly reported as non-valid<br />");
else {
isUK = (data[count].isUKEmail()) ? 'UK' : 'foreign';
document.write("OK: the email <strong>" + data[count] + "</strong> recognised correctly, " + isUK + "<br />");
}
};
// exmaple where it only checks for a valid uk email.
document.write("<br/><br/>");
count = data.length;
while (count--) {
if (data[count].isValidUKEmail())
document.write("UK email: <strong>" + data[count] + "</strong><br/>");
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.