SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

    Where in this preg_match is it saying no empty value?

    Where in this Clean Email preg_match is it saying no empty value?

    Code PHP:
    // Clean Email
    if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$email)) {
        header( "Location: $errorurl" );
        exit ;
    }

    I already use this statement to clean empty values so I would like to remove it from the above statement. Do you know how?

    Code PHP:
    // Clean All
    if (empty($name) || empty($email) ||empty($comments)) {
    	header( "Location: $errorurl" );
    	exit ;
    }

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Let's break apart your regex
    Code:
    ^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
    ^ means, what follows should be at the beginning of the string provided
    [A-Z0-9._%-]+ means to find 1 or more occurrences of letters, numbers, ., _, %, or -
    @ means to find the @ symbol after you find one or more of the occurrences of the line above.
    [A-Z0-9.-]+ means to find 1 or more occurrences of letters, numbers, ., or - following the @ symbol
    \. means to find a . after the line above
    [A-Z]{2,4}$ lastly, find 2 to 4 characters that are after the . and are at the end of the string


    So you can deduce by the above explanation, that you won't get a match for "" because it fails to find 1 or more characters at the beginning of a string that are letters, numbers, ., _, %, or -. Plus it fails to have the @ in after find those said beginning letters and it fails to have the required . before the suffix of the domain (which also must exist as the end of the string)
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Uhmmm... Ok... So how should it be rewritten to say if empty ignore all - and if value entered clean as usual?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // Clean Email
    if (!empty($email) && !preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$email)) { // verify the email is not empty, check if it passes the regex
        
    header"Location: $errorurl);
        exit ;

    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    PHP Code:
    // Clean Email
    if (!empty($email) && !preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$email)) { // verify the email is not empty, check if it passes the regex
        
    header"Location: $errorurl);
        exit ;

    If I'm reading this wrong please tell me, but I need it to do NOTHING if empty and then CLEAN as usual if a value is entered?

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,070
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    The code states, only if there is a value in $email, run it against the preg_match expression, otherwise, continue on with the script execution.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Ahhh ok I'm good to go then! Thank you very much for your help.


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
  •