Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: email validation in php

  1. #1
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default email validation in php

    hey im trying to put email validation in php, i tried searching and the code i found does not work it shows 500 internal error here is my code
    PHP Code:
    <?php 

    $to 
    ""
    $subject "subject";
    $mail $_POST['mail']; 

    if (
    $mail $_POST['mail']) { 

    $body "E-Mail: $mail"

    echo 
    "success";

    mail($to$subject$body"From: $mail"); 
    }  else { 
    echo 
    "error";

    ?>
    and here is the code im trying to add
    http://www.go4expert.com/forums/showthread.php?t=1128

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    you can use this code to validate an email address

    PHP Code:
    $result preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i'$email); 
    If $result is TRUE then proceed if FALSE then error

  3. #3
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    and where should i use this $result?

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This is where to use it, note the comment saying it can replace your 'if' statement.

    PHP Code:
    <?php  

    $to 
    "";  
    $subject "subject"
    $mail $_POST['mail'];  
    $result preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i'$mail);  

    if (
    $result){
        
    if (
    $mail $_POST['mail']) {  /*Why this line it will always be true, you can replace it with the 'if' above*/

    $body "E-Mail: $mail";  

    echo 
    "success"

    mail($to$subject$body"From: $mail");  
    }  else {  
    echo 
    "error"
    }  
    ?>
    If you are submitting the email from an HTML form then personally I prefer to do any validation at the input stage.

    It is fairly easy to use javascript to field validate a form

  5. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    ff123 (05-14-2009)

  6. #5
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This test is probably better, it stops consecutive '.'

    $result = preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i', $mail);

  7. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    When possible, you should use the filter extension over regex.
    http://php.net/filter

  8. #7
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by forum_amnesiac View Post
    This is where to use it, note the comment saying it can replace your 'if' statement.

    PHP Code:
    <?php  

    $to 
    "";  
    $subject "subject"
    $mail $_POST['mail'];  
    $result preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i'$mail);  

    if (
    $result){
        
    if (
    $mail $_POST['mail']) {  /*Why this line it will always be true, you can replace it with the 'if' above*/

    $body "E-Mail: $mail";  

    echo 
    "success"

    mail($to$subject$body"From: $mail");  
    }  else {  
    echo 
    "error"
    }  
    ?>
    If you are submitting the email from an HTML form then personally I prefer to do any validation at the input stage.

    It is fairly easy to use javascript to field validate a form
    thanks alot... well i was using javascript validate thingy, but i wanted with php.. thanks anyway

  9. #8
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    hey again.. one more thing im using this for my contact, where should i put that email validation thing so it can even validate in here..

    PHP Code:
    if ( $message $message && $name $name && $fone $fone && $mail $mail ){

    $body "From: $name\n E-Mail: $mail\n Phone: $fone\n Message:\n $message";
    } else {
    echo 
    ""

    Last edited by ff123; 05-14-2009 at 05:38 AM.

  10. #9
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Try this

    PHP Code:
    $result preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i'$mail); 

    if (
    $result){
    if ( 
    $message $message && $name $name && $fone $fone && $mail $mail ){ 

    $body "From: $name\n E-Mail: $mail\n Phone: $fone\n Message:\n $message"
    } else { 
    echo 
    "" 
    }  
    } else {
    echo 
    "" 

    It may easily be possible to write this neater but it should work

  11. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    ff123 (05-14-2009)

  12. #10
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    thats cool mate.. it works but please tell me what is the main difference between

    PHP Code:
    preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/"$mail 
    and

    PHP Code:
    $result preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i'$mail); 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •