...

View Full Version : A question about my regex



jeddi
01-11-2010, 01:13 PM
Hi I am using this to try and check for
properly constructed email addresses but I keep getting an error:



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. :confused:

Would appreciate a second pair of eyes :)

JAY6390
01-11-2010, 02:41 PM
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() (http://www.php.net/filter_var) function's built in e-mail address validator with the constant FILTER_VALIDATE_EMAIL as the filter. Check out here (http://www.php.net/manual/en/filter.filters.php) for all the filter types

Fou-Lu
01-11-2010, 02:44 PM
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.

jeddi
01-11-2010, 03:49 PM
thanks for the input :thumbsup:

So is this the correct format?

İnstead of this:


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:


if (!filter_var($N_email, FILTER_VALIDATE_EMAIL)) {
echo "email problem";
}

May be it is faster as well ?

JAY6390
01-11-2010, 03:51 PM
Speed wise I'm not 100% but it's definitely more reliable. And yes the regex is fine

jeddi
01-11-2010, 04:57 PM
Thanks for your help,
but I am getting strange results :confused:

This is my code:




// Test for valid email address.
if (!filter_var($N_email, FILTER_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 ?

JAY6390
01-11-2010, 05:02 PM
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)

jeddi
01-11-2010, 05:37 PM
Thanks

I needed to trim the input
first.

JAY6390
01-11-2010, 05:38 PM
Ah OK. Glad you got it sorted :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum