PHP Classes

Issue with e-mail address validation

Recommend this page to a friend!

      PHP Secure Login and Registration  >  PHP Secure Login and Registration package blog  >  Secure PHP Login Scri...  >  All threads  >  Issue with e-mail address validation  >  (Un) Subscribe thread alerts  
Subject:Issue with e-mail address validation
Summary:The regular expression is not the right one
Messages:3
Author:Raphaël Rousseau
Date:2016-12-21 11:17:32
 

  1. Issue with e-mail address validation   Reply   Report abuse  
Picture of Raphaël Rousseau Raphaël Rousseau - 2016-12-21 11:17:32
Hi,

The validateEmail function isn't the right method for validating a text variable as an e-mail address.
For instance, it would return FALSE for "john@mycompany.consultant", since if restrict TLD to 4 alphanumeric characters.

The right method is through the filter_var() function:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}


  2. Re: Issue with e-mail address validation   Reply   Report abuse  
Picture of Ashraf Gheith Ashraf Gheith - 2016-12-21 12:22:59 - In reply to message 1 from Raphaël Rousseau
Thank you for the note, I will modify that :)

  3. Re: Issue with e-mail address validation   Reply   Report abuse  
Picture of Ashraf Gheith Ashraf Gheith - 2016-12-21 12:48:46 - In reply to message 2 from Ashraf Gheith
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}

it is now.

function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,})?$/;
return emailReg.test( $email );
}

so it is not limited to 4 chars only.
And in PHP side I am already using that function. This is JS.