How to validate an email address in php?
- Validate email using Regular Expression
- Validate email address using filter_var() built-in php function
1. Validate an email address using regular expression with php.
PHP Code
function validateEmailAddressUsingRegularExpression($emailAddress){ $regularExpression='/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/'; if (preg_match($regularExpression,$emailAddress)) { return true; }else{ return false; } }
$emailAddress='someone@gmail.com';
if(validateEmailAddressUsingRegularExpression($emailAddress)==false){ echo 'Email address in invalid'; }else{ echo 'Email address is valid'; }
2. Validate an email address using filter_var() function in php.
PHP Code
function validateEmailAddressUsingFiltervar($email_address){ if(!filter_var($email_address, FILTER_VALIDATE_EMAIL)){ return true; }else{ return false; } }
$emailAddress='someone@gmail.com';
if(validateEmailAddressUsingFiltervar($emailAddress)==false){ echo 'Email address in invalid'; }else{ echo 'Email address is valid'; }
0 comments:
Post a Comment