Jump to content

Recommended Posts

Hi,

I tested this expression to see if it works before I use it for  validating.  I don't understand why it returns: "invalid email id" Can anyone tell me?  I'm not very good with regular expressions.

 

$email = "myemail@gmail.com";


if (ereg('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$','$email'))
{
echo 'Valid email id';
} else {
echo 'Invalid email id';
}

 

Link to comment
https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/
Share on other sites

ereg is a deprecated function, you should probably look at using preg_match instead. But I believe your problem is where you match the fullstop between the domain and the domain extension. Since the fullstop is not escaped it will match any character, which will mess up the pattern. but a backslash in before it should fix that.

ereg is a deprecated function, you should probably look at using preg_match instead. But I believe your problem is where you match the fullstop between the domain and the domain extension. Since the fullstop is not escaped it will match any character, which will mess up the pattern. but a backslash in before it should fix that.

 

not escaping the dot would not break it per se (it wouldn't break it in this instance, anyways).  It'll match (most) anything including a literal dot.  Danger of not escaping it is that it can match more than a literal dot.

 

anywhoo...your regex is failing because of this:

 

'$email'

 

You have $email wrapped in single quotes which causes ereg to interpret literally.  Remove the quotes.

 

Also as cags mentioned, move to preg_match instead of ereg. Only thing you really need to change for that is you need to add delimeters to your regex.

You're right, I was thinking of matching an email within as string not matching an e-mail as a string (ie I was ignoring the ^ and $). So with everything being greedy it would match pretty much anything after the @ (except line breaks). Good catch on the single quotes, I didn't even notice that.

You're right, I was thinking of matching an email within as string not matching an e-mail as a string (ie I was ignoring the ^ and $). So with everything being greedy it would match pretty much anything after the @ (except line breaks). Good catch on the single quotes, I didn't even notice that.

 

took me a min... I was about to vote for suggesting he trim $email then at last second noticed.

I tried:

$email = "myemail@mydomain.com";

if (preg_match('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',"$email"))
{
echo 'Valid email id';
} else {
echo 'Invalid email id';
}

and now getting:

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in....myfile etc

One difference between the preg functions and the older functions is that patterns must have delimiters. It thinks you are using the ^ character as the delimiter since that is the first character. But you actually wish that to be the first character of your pattern. You simply need to add delimiters...

 

if (preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~', $email))

 

... incidently I see you change the single quotes around $email to double quotes. You really might aswell just get rid of the quotes altogether (as I did in my example).

 

Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details.

Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details.

 

Studies have shown that real regex men use hash instead (and I'm not only refering to delimiters either). :sarcastic:

Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details.

 

Studies have shown that real regex men use hash instead (and I'm not only refering to delimiters either). :sarcastic:

 

ooh I think you finally won me over to using # instead of ~  I can say "I like hash" :P

if (preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~', $email))

 

Worked! Thanks.

Will Zen(v) this for a while.

Some good regexp info at PHPFKS.

 

About the hash:

"I tried smoking hash but the corned beef wouldn't stay lit"

 

-forgot who said that originally

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.