Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,093
    Thanks
    2
    Thanked 23 Times in 23 Posts

    Validating an e-mail address

    Here's a slick way of validating an email address that someone passes you through your contact form:
    Code:
    if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $from)) {
       // code to send e-mail goes here
    }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I'd change a few things.
    PHP Code:
    $email trim($email);
    if (
    preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.([a-z]{2,4})$/i'$email)) {
       
    // code to send e-mail goes here

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Still, if you use this, understand that it is not infallible.(sp?) All it does is screen out someone from typing "dga;sfgharggnjg" into the email box. a@b.c would beat the script, as would 1@2.3. But, this script is still great at screening out some nonsense submissions.

    Dan

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,093
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Quote Originally Posted by whizard
    a@b.c would beat the script, as would 1@2.3. But, this script is still great at screening out some nonsense submissions.
    Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.

    What might be interesting is using the Validate PEAR package. I haven't tried that, but it's supposed to somehow validate the domain as well as the email address.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vinyl-junkie
    Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.

    What might be interesting is using the Validate PEAR package. I haven't tried that, but it's supposed to somehow validate the domain as well as the email address.
    Actually I believe it's 2-4 chars after the period for the TLD. 1@2.aa would work.
    "$question = ( to() ) ? be() : ~be();"

  • #6
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,093
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Quote Originally Posted by Velox Letum
    Actually I believe it's 2-4 chars after the period for the TLD. 1@2.aa would work.
    Proving once again that I'm terrible about interpreting regular expressions.

    I just tested your example, and it does pass the edits in my contact form.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There also needs to be a clause written into the regex to allow an additional period and 2-4 chars for domains such as .co.uk. I wrote the clause below:

    PHP Code:
    $email trim($email); 
    if (
    preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.([a-z]{2,4})($|\.([a-z]{2,4})$)/i'$email)) { 
       
    // code to send e-mail goes here 

    "$question = ( to() ) ? be() : ~be();"

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It has worked with .co.uk before.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It doesn't seem like it would, as a period is not allowed and co.uk is five characters. *shrug*
    "$question = ( to() ) ? be() : ~be();"

  • #10
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It's becouse the .co matches with the "[a-z0-9._-]+" part just after the @.
    From:
    email@site.co.uk
    it matches like this:
    [a-z0-9._-]+ => email
    @ => @
    [a-z0-9._-]+ => site.co
    \. => .
    ([a-z]{2,4}) => uk
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #11
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    It'd probably be best to modify it to comply to RFC 2822, which is more or less the rules as to what is, and what isn't a valid email address. Examples are: user@example.com, User <user@example.com>

  • #12
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    It's becouse the .co matches with the "[a-z0-9._-]+" part just after the @.
    From:
    email@site.co.uk
    it matches like this:
    [a-z0-9._-]+ => email
    @ => @
    [a-z0-9._-]+ => site.co
    \. => .
    ([a-z]{2,4}) => uk
    Ahh I see. My apologies.
    "$question = ( to() ) ? be() : ~be();"

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Quote Originally Posted by vinyl-junkie
    Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.
    Sorry, I misread the code.

    Quote Originally Posted by vinyl-junkie
    Proving once again that I'm terrible about interpreting regular expressions.
    Don't feel so bad

  • #14
    New Coder
    Join Date
    Nov 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the expression I use to check against email address submissions...
    PHP Code:
    preg_match("/^(([^<>()[\]\\\\.,;:\s@\"]+(\.[^<>()[\]\\\\.,;:\s@\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i"$_POST['email_address']) 
    Last edited by Nikolis; 11-15-2005 at 10:08 PM.

  • #15
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by Error 404
    It'd probably be best to modify it to comply to RFC 2822, which is more or less the rules as to what is, and what isn't a valid email address.

    Fwiw…

    http://www.regexlib.com/REDetails.aspx?regexp_id=711
    Code:
    ^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$
    http://www.regexlib.com/REDetails.aspx?regexp_id=26
    Code:
    ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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