Tuesday 24 November 2009
Email Address Format Function
I'm not even going to try and explain this regular expression ... what matters is that it works!
PHP
function invalidEmail($str) {
if (preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", $str)) return false;
else return true;
}JavaScript
function invalidEmail(str) {
if (str.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/))
return false;
else return true;
}These functions return true if the input is invalid, as they are error-checking functions. The code can then be written if (invalidEmail(str)) which is nice and easy to read.
Next article: PHP Email Read Receipt (24 November 2009)
Next Websites article: Mailto: Syntax (16 March 2010)
--- Posted in Websites and viewed 7,344 times ---