Tuesday 24 November 2009

Email Address Format FunctionEmail Address Format Function

Left QuoteI'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.Right Quote

Next article: PHP Email Read Receipt (24 November 2009)

Next Websites article: Mailto: Syntax (16 March 2010)

CommentsComments

Add your comments

Name

Comments

Question

3 * 1 = (this security question stops automated submissions)