#1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    I'd like to check (for security purposes) that an email address entered in a text input is valid (ie it contains at least one . which must be after the @ and contains no semi-colons). How can I do this in PHP? I figured it can be done with Regular Expressions but they're not very well-documented in my PHP book.

    Thanks in advance,

    --------------------------------------------
    Alex
    (http://www.alex-greg.co.uk)
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    This is a javascript validation - but I do my validation client-side anyway, but I am sure the regex would translate well enough. If you prefer PHP validation search at www.hotscripts.com or www.weberdev.com or www.phpbuilder.com they all have validation scripts listed for PHP.

    <script>
    var re = new RegExp("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$");
    function val(){
    if(mail=document.validation.mail.value==""){return true;} else {
    mail=document.validation.mail.value;if(!re.test(mail)){alert("Very funny - that is not a valid e-mail format, leave the email empty OR supply a valid e-mail address");return false;} else {return true;} }
    }</script>

    that particular script allows for either a valid address or none at all but you should get the drift.

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    33
    Rep Power
    15
    Refer webmonkey tutiorials.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 1999
    Location
    Perth, Western Australia
    Posts
    53
    Rep Power
    16
    This functions works for me:

    function val_email($email) {
    if (eregi("^[^@[:space:]]+@([[:alnum:]-]+.)+[[:alnum:]][[:alnum:]][[:alnum:]
    ]?$", $email)):
    return TRUE; // the address is good
    else:
    return FALSE; // the address is bad
    endif;
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    139
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <input type="textbox" name="email">

    <?
    $test = eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email);
    if ($test)
    echo "E-Mail is valid";
    else
    echo "Invalid E-Mail";
    ?>
    [/code]

    [This message has been edited by Tsigo (edited August 15, 2000).]
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16
    eregi("^[^@[:space:]]+@([[:alnum:]-]+.)+[[:alnum:]][[:alnum:]][[:alnum:]
    ]?$", $email))


    This won't work with addresses like bob@123.123.123.123 or bob@school.k-12.ca.us, both of which are legal email addresses.

    Don
  12. #7
  13. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    Another sample script...


    <?php

    $email="shiju@postcom";

    if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+.)*([a-z0-9-]+)(.[a-z]{2,3}))$', $email)){
    echo "Not a valid email addressn";
    }else{
    echo "It is a valid email!!";
    exit;
    }

    ?>


    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."

Similar Threads

  1. inserting multiple email address from a textarea
    By mrkgrav in forum PHP Development
    Replies: 17
    Last Post: September 24th, 2004, 07:28 PM
  2. Regular Expression for multiple email address
    By L John in forum HTML Programming
    Replies: 2
    Last Post: January 29th, 2004, 07:41 AM
  3. HTML: submitting forms to hidden email address?
    By kitiwiti in forum Beginner Programming
    Replies: 4
    Last Post: December 21st, 2003, 10:17 PM
  4. need email address from submitted html email form
    By fallen8ngel in forum PHP Development
    Replies: 4
    Last Post: November 22nd, 2003, 04:11 AM
  5. breaking up an email address from a DB
    By Fife Club in forum MySQL Help
    Replies: 2
    Last Post: November 17th, 2003, 11:53 PM

IMN logo majestic logo threadwatch logo seochat tools logo