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.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    A question about my regex

    Hi I am using this to try and check for
    properly constructed email addresses but I keep getting an error:


    PHP Code:
    if (!preg_match('@^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$@',$N_email) ){ 
    The error is:
    Warning: preg_match() [function.preg-match]: Unknown modifier '[' in /home/guru54gt5/public_html/im/index1.php on line 64

    I can not see what I have done wrong in my regex.

    Would appreciate a second pair of eyes

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You've use @ as your delimited and you have one half way through the regex. You need to either change your delimited or escape the @ using a backslash. On a side note, you are much better off using the filter_var() function's built in e-mail address validator with the constant FILTER_VALIDATE_EMAIL as the filter. Check out here for all the filter types
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You're bounding delimiters are the same as a matching character, so it considers the @ to be the end delimiter for you're pattern.
    Either change them or escape you're @ sign in the middle of you're pattern.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    thanks for the input

    So is this the correct format?

    İnstead of this:

    PHP Code:
    if (!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/',$N_email) ){
      echo 
    "email problem";

    Should I write this:

    PHP Code:
    if (!filter_var($N_emailFILTER_VALIDATE_EMAIL)) {
      echo 
    "email problem";

    May be it is faster as well ?

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Speed wise I'm not 100% but it's definitely more reliable. And yes the regex is fine
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your help,
    but I am getting strange results

    This is my code:


    PHP Code:
    // Test for valid email address.
    if (!filter_var($N_emailFILTER_VALIDATE_EMAIL)) { 
      
    $message1 "$N_email, Please enter your correct email address.";
       require_once (
    "index1_fm.php");
       exit();
      }  
    // endif 
    I assume that -ve ( ! ) is correct ?

    My form keeps rejecting it.

    I have echoed the input email address
    and it shows valid address ???

    Have I done something wrong ?

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    No that is fine. If you do a var_dump on $N_email it will show you the exact content of the variable (be sure to check it in view source to make sure there are no extra spaces or line breaks)
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks

    I needed to trim the input
    first.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Ah OK. Glad you got it sorted
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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