Jump to content

[SOLVED] validate emails like test@test


ratcateme

Recommended Posts

I am wanting to validate emails i have searched all over google and stuff i can find plenty of ways to validate emails like test@test.com but they wont validate emails like test@test as well. i am just looking for a expression to validate both at the same time or i can do each like this

if(preg_match(test@test) || preg_match(test@test.com))

so far i have this

^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]

i tried to modify it to

^[a-zA-Z0-9_]+@[a-zA-Z0-9\-\.]+$]

but that wont work.

 

Scott.

Link to comment
Share on other sites

try

<?php
$test = array('test@test', 'test@test.com', 'test@test@test.com', 'john@google.com.com');
foreach ($test as $a){
echo $a, ' - ';
echo preg_match('/^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]+$/', $a), "<br />\n";
}

?> 

Link to comment
Share on other sites

How about this? /^\w+@(?:\w+\.)+\w+$/

 

 

 

1 or more letters/numbers

@

1 or more letters/numbers

.

1 or more letters/numbers

 

 

What about john.smith@host.com, or john@users.sourceforge.net (using the earlier example)?  Or am I reading the regexp incorrectly?

Link to comment
Share on other sites

I'm more interested as to why you want to validate test@test.

This is based on the RFC standard and should catch ANY valid email.

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(??:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Link to comment
Share on other sites

I'm more interested as to why you want to validate test@test.

 

Because the syntax is user@hostname. Thus if the hostname is daniel-laptop and the user is daniel then the address would be daniel@daniel-laptop. If the hostname is gmail.com and the user is daniel.egeberg then the address is daniel.egeberg@gmail.com. Simple, right?

 

Example:

 

daniel@daniel-laptop:~$ mail
No mail for daniel
daniel@daniel-laptop:~$ mail daniel@daniel-laptop
Subject: Test Email
This is a test
Cc: 
daniel@daniel-laptop:~$ mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/daniel": 1 message 1 new
>N  1 daniel@daniel-lap  Sun Aug  3 20:13   14/476   Test Email
& 

Link to comment
Share on other sites

i put that expression into the code above i think i got the escaping wright here's what i go

preg_match("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(??:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", $a)

it said

Unknown modifier '@' 

i tried PHPTOM's suggestion but that says test@test is not valid

 

Scott.

Link to comment
Share on other sites

While this solution is not so much hardcoded preg expression stuff, it does the trick. This is what I ran into recently:

 

$email = filter_var(filter_var($_POST['email'], FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
if($email === false){
   return false;
} else {
   return true;
}

 

One big area of concern with expressions is how 'strict' some can be. The general consensus is that allowing some flexibility is more preferable than locking out potential email validations due to extrememly tight-fisted expression routines. So PHP has these nice and neat little FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL functionalities that make for some relaxed enough checking to as to be reasonable enough, yet be strict enough at the same time...

 

Cheers,

 

NRG

Link to comment
Share on other sites

thanks for the reply but that still says test@test is invalid. does anyone know how i could use this one

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)

with another one that will validate emails like test@test so my script look like

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) || preg_match(test@test))

 

Scott.

Link to comment
Share on other sites

What's wrong with the one I gave you? I tested it and it does match the emails you want:

 

Try /^(?:\w+.?)+@(?:\w+.?)+$/

 

function test($email) {
echo $email . ': ';
if (preg_match('/^(?:\w+.?)+@(?:\w+.?)+$/', $email)) {
	echo 'valid';
}
else {
	echo 'invalid';
}
echo PHP_EOL;
}

test('test@test');
test('test.hello@test');
test('test@test.hello');
test('test.hello@hello.test');

 

Output:

test@test: valid
test.hello@test: valid
test@test.hello: valid
test.hello@hello.test: valid

 

By the way, the PCRE functions (e.g. preg_match()) are faster than the POSIX regex functions (e.g. ereg()).

 

Edit: Actually, it should be /^(?:\w+\.?)+@(?:\w+\.?)+$/ instead. Otherwise the period (.) will act as a metacharacter meaning "any character", but we want it to be a literal period.

Link to comment
Share on other sites

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.