We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33372
    • 1,611 Posts
    Can anyone see why this regular expression would reject uppercase letters in email addresses?

    						case "email":
    							//stricter email validation
    							if (strlen($value)>0 &&  !preg_match(
    							'/^(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?@(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?\.[a-z0-9]{2,5}$/i', $value) ) $vMsg[count($vMsg)]=$desc . $_lang["ef_invalid_email"];
    							break;


    I’m finding that uppercase characters cause emails to be rejected as invalid, but damned if I can see why. The expression ends with /i, which should make it case-insensitive, right? And yet if I enter [email protected] instead of [email protected] eForm is saying that my email is invalid...

      "Things are not what they appear to be; nor are they otherwise." - Buddha

      "Well, gee, Buddha - that wasn't very helpful..." - ZAP

      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
      • 30223
      • 1,010 Posts
      Are you sure there’s nothing else going on? I just made a little test script around this line and both variants are valid.

      <?php
      
      $emails = array(
      	"[email protected]",
      	"[email protected]",
      	"[email protected]",
      	"[email protected]"
      );
      
      foreach( $emails as $value){
      	if (strlen($value)>0 &&  !preg_match('/^(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?@(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?\.[a-z0-9]{2,5}$/i', $value) )
      		print "$value is <strong>not valid!</strong><br />";
      	else
      		print "$value is <strong>valid</strong><br />";
      }
      
      
      /*
      results:
      [email protected] is VALID
      [email protected] is VALID
      [email protected] is VALID
      [email protected] is NOT VALID
      */
      ?>
      
        • 33372
        • 1,611 Posts
        Oops! Silly me. I was looking at the wrong version of eForm. I have it installed on so many different sites now, and of course they’re not all the same. The version that is actually giving me trouble has this code instead:

        					case "email":
        						if ($isRequired==1 && $value=="") $vMsg[count($vMsg)]="$desc is required";
        						else if (strlen($value)>0 && !preg_match('/^[a-z0-9_.-]+@[a-z0-9.-]+\.[a-z]+$/',$value)) $vMsg[count($vMsg)]="$desc is not a valid email address";
        						break;


        ...so of course it is missing the "i" at the end of the expression to make it case-insensitive.

        SORRY I wasted your time checking out a false issue!
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options