Allowing apostrophes(') in a valid email address in codeIgniter

I have recently tried to validate the email of users using CI "Form_validation" Library and have found that using the valid_email it failed quite a few addresses that had apostrophe in their email address such as salman.D'Ahmad@example.com

I already added the apostrophe in there regular expression but still its not validate the email address with apostrophe(') .



Orignal Code:

public function valid_email($str)
{
         return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]                          {2,6}$/ix", $str)) ? FALSE : TRUE;
}


Solution : This is a very small change if you see first by just adding the apostrophe in regex expression but its not working like below :


Change Code:


public function valid_email($str)
{
       return ( ! preg_match("/^([a-z0-9\+_\-']+)(\.[a-z0-9\+_\-']+)*@([a-z0-9\-]+\.)+[a-z]                        {2,6}$/ix", $str)) ? FALSE : TRUE;
}


Final Solution:

public function valid_email($str)
{
$str = str_replace(''', "'", $str);
return ( ! preg_match("/^([A-Za-z0-9\+_\-']+)(\.[A-Za-z0-9\+_\-']+)*@([a-z0-9\-]+\.)+                   [a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}



chears ...
Happy coding :)

Post a Comment

Previous Post Next Post