Page 1 of 1

preg_match validation

Posted: Sun Mar 12, 2006 4:38 pm
by duk
hy

i have read some documents about email validations where i cant find now in this forum,

i have this 3 functions

Code: Select all

function validate_email($emailfrom) {
          return preg_match("/^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$/",$emailfrom);
      }
      
      function has_no_emailheaders($emailfrom) {
          
          return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $emailfrom);
      }
      
      function has_no_newlines($emailfrom) {
          
        return preg_match("/(%0A|%0D|\n+|\r+)/i", $emailfrom);
      
      }
where if i try to

$_POST['email'] = "mail@mail.com"; // its what is being posted

echo "1".$obj_mail->validate_email($_POST['email']);
echo "2".$obj_mail->has_no_emailheaders($_POST['email']);
echo "3".$obj_mail->has_no_newlines($_POST['email']);

the result is always 0 in the 3 functions, its expcted the first function return 1 where it matches... ??? but anyway if i try to put \n or cc in the email all 3 functions returns 0 allways... and should return 1 in case the is a match right ??

Posted: Sun Mar 12, 2006 8:23 pm
by feyd
a dot without escape matches any character. That's why it fails.

viewtopic.php?p=239565#239565

Posted: Mon Mar 13, 2006 3:44 pm
by duk
are you saying that mail@mail.com\n is a valid email ??

Posted: Mon Mar 13, 2006 3:49 pm
by feyd
I said nothing of the sort.

Code: Select all

$a = '.'; // match any character
$b = '\.'; // match a dot

Posted: Mon Mar 13, 2006 4:07 pm
by duk
i don't know what are you saying... :(

i have change the preg_match for the email validation

preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i",$emailfrom);

bt i cant understand why mail@mail.com\n is not returning 1... wth %0A returns 1

Posted: Mon Mar 13, 2006 4:57 pm
by John Cartwright
Moved to Regex.

Posted: Mon Mar 13, 2006 6:23 pm
by duk
ok should be a \. to match a dot and not any character... need to test that, going sleep