...

View Full Version : Preg Match Problem.



heaps21
10-11-2004, 08:16 PM
Hi everyone, i have teh following function using preg_match to validate email addresses:


function validate_email($email)
{
$email = trim( $email ); # removes whitespace
if((empty($email)) || (!preg_match('/^[a-z0-9\\_\\.]+@[a-z0-9\\-]+\\.[a-z]+\\.?[a-z]{2,4}$/i', $email, $match))){
return 1;}
}


Unfortunately it thinks email addresses of the form abc@xyz.fsnet.co.uk are invalid, which they arent. I have been trying to modify it so that all forms of email addresses from 'abc@xyz.com' to 'abc@xyz.fsnet.co.uk' are allowed, but with regex being my major downfall I havent been having much luck. If anyone can quickly solve my problem I would be most grateful.

Many Thanks,
Andy.

trib4lmaniac
10-11-2004, 08:24 PM
^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$

or something like that.

cyphix
10-11-2004, 08:29 PM
Heres one I use for email addresses:

if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)) {


That what u looking for?

trib4lmaniac
10-11-2004, 08:44 PM
preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)
Bet that matches alot :p

Fou-Lu
10-12-2004, 05:12 PM
Heres one I use for email addresses:

if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)) {


That what u looking for?

Am I on crack, or do I see that the email needs to match any character that does not begin with an alphanumeric character, an underscore, a hyphen, a period, or an @ symbol?

Lets see here....
Hmm, give this a shot:


if (preg_match('/^([a-z0-9_-]+)(\.[a-z0-9_-]+)*@([-a-z0-9]+\.?)+\.[a-z]{2,4}$/i', $email))
{
// Blah blah blah...
}


or... if the perl delimiters work, which they should:


if (preg_match('/^([-\w]+)(\.[-\w]+)*@([-a-z0-9]+\.?)+\.[a-z]{2,4}$/i', $email))
{
// Blah blah blah...
}


Yeah, so that should work as long as the email begins with an alphanumeric character, an underscore or hyphen, followed by any amount of characters after that point seperated by a period, and ending with your server domain. Its been awhile, but I believe that email address can contain those characters, but the server cannot have the underscore, correct me if I'm wrong. Oh, I haven't tested that btw, so if you try it let me know so that I can use it too :p

cyphix
10-12-2004, 05:58 PM
Am I on crack, or do I see that the email needs to match any character that does not begin with an alphanumeric character, an underscore, a hyphen, a period, or an @ symbol?

You're on crack.. :p

No, it says... if $email contains anything but those charcters then do whatever....

trib4lmaniac
10-13-2004, 09:49 AM
You're on crack.. :p

No, it says... if $email contains anything but those charcters then do whatever....
Not very accurate then

cyphix
10-13-2004, 10:32 AM
Not very accurate then

How is it not accurate?

I wanna check it's a valid email so if it contains anything but those characters then it'll produce an error.

Fou-Lu
10-13-2004, 11:42 AM
How is it not accurate?

I wanna check it's a valid email so if it contains anything but those characters then it'll produce an error.

Because its not accurate. Your replacement technique specifies that if it will only match a string containing a symbol in it, excluding the underscore, hyphen, and the @ symbol.


if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)) {

}


This will almost always return false. Unless the forums stripped the \ in front of the ^, then its all good, and would match almost any string period. Otherwise I see little use for such a matching pattern.

Example of what that matching will return on:


if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email))
{
return true;
}

The following will return valid:
$email = '!';
$email = '###$%***';
Examples of what will not work correctly:
$email = 'you@yourdomain.com';
$email = 'yourdomain';

I'm confused as to how this can be used to correctly validate an email.

cyphix
10-13-2004, 11:56 AM
I think I see what you're saying..... now that I recall, I use this for making sure the email address doesn't contain any characters it shouldn't & only valid characters that are allowed in email addresses.

I also use this one to check it has a '@' symbol..



if (strpos($email, "@") == false) {

cyphix
10-13-2004, 11:59 AM
The following will return valid:
$email = '!';
$email = '###$%***';
Examples of what will not work correctly:
$email = 'you@yourdomain.com';
$email = 'yourdomain';


Quite the contrary actually ;)

I'm looking for ERRORS in that code..... as explained in the above post.... not saying "IF it's alright then continue on"..... saying "IF it contains anything but these characters" then we have a problem..

Fou-Lu
10-13-2004, 12:04 PM
Thats the problem though, there isn't enough information to base off of. Even with the added strpos, a lot will still work, to say that its valid when it truely is not.

$email = !@;
will return true.

cyphix
10-13-2004, 12:07 PM
Yes. it will.

But this is enough validation for me seeing as I also use email validation with a link they click in their email.... but I guess that's not exactly what we're getting at here. ;)

Fou-Lu
10-13-2004, 04:52 PM
Ah, I sees, you are pretty much validating that it has the @ in it. That makes sense then. But yeah, for the application needed, I would recommend a more extensive pattern matching technique.

trib4lmaniac
10-13-2004, 08:31 PM
function validate_email($address) {
$address = preg_replace('/"[^"]*"/', '', $address);
return preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$/i', $address);
}
I think that should match pretty much all types of email around. Not tested though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum