31

Is there any rule for having a specified amount of @ symbol in any email id. Just come to my mind if we're to check if an email id is valid or not using PHP.

droidlabour
  • 567
  • 2
  • 8
  • 21

6 Answers6

40

If enquoted multiple @ are allowed. I have to ask why do you need this information. Please please please do not try to write a regex / function or whatever to validate emailaddresses. You have to worry about 3 rfc's (and perhaps 4 if you want to take into account unicode). And you will fail and it will drive you mad.

See my previous answer for more information and a regex you may use if you have an older version of PHP

P.S.

In case someone missed my point: do not try to come up with some validation of your own to validate emailaddresses yourself "ever".

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • Double checking here, you mean for '@' signs in the *username* there can be multiple right? There can't be an '@' in the domain can there? The reason I ask is for obtaining hostnames from submitted emails to check mx records, is as far as I know, the standard validation scheme. – Jamie Marshall Dec 13 '20 at 19:55
  • Following your last point... Rather than spend the time attempting to validate an address with regex etc, the norm is to send the address in question an email with a link to validate. The end user clicks the link and this serves as validation that 1) the email address is real and exists and 2) is also actively used (relatively speaking) and accessible by the user. From a database perspective, a validation key can be stored alongside the user account along with a flag of invalid or valid state. – ThisClark May 25 '22 at 15:18
26

There can be any number (within the size limits of an email address) except that the last one must be the separator between the domain name and the "local part".

If an @ or any other "unusual" characters (as defined by RFC 5322) exist in the local part it MUST appear in quotes, e.g: "user@something"@example.com

Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
19

There is no specific limit on @ characters.

"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com is a valid address.

See also, the Wikipedia page on the subject.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

An addr-spec is a specific Internet identifier that contains a locally interpreted string followed by the at-sign character ("@", ASCII value 64) followed by an Internet domain. The locally interpreted string is either a quoted-string or a dot-atom. If the string can be represented as a dot-atom (that is, it contains no characters other than atext characters or "." surrounded by atext characters), then the dot-atom form SHOULD be used and the quoted- string form SHOULD NOT be used. Comments and folding white space SHOULD NOT be used around the "@" in the addr-spec.

See :- https://www.rfc-editor.org/rfc/rfc5322#section-3.4.1

Community
  • 1
  • 1
Shail
  • 1,565
  • 11
  • 24
1

My real question is would it make sense to have two @ symbols in an email?
How would it work?

I would use filter_var() with FILTER_VALIDATE_EMAIL. Below is a sample use and outputs,

function testemail($email) {
    echo "'$email' => ";
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));    
    echo "<br />";
}

testemail('');
testemail('myemailsample.com');
testemail('myemail@sample');
testemail('myemail@sample.com');
testemail('myemail@@sample.com');
testemail('myemail@sa@mple.com');

Output,

'' => bool(false)
'myemailsample.com' => bool(false)
'myemail@sample' => bool(false)
'myemail@sample.com' => string(18) "myemail@sample.com"
'myemail@@sample.com' => bool(false)
'myemail@sa@mple.com' => bool(false)

Jared Paolin
  • 247
  • 1
  • 3
  • 3
    "My real question is would it make sense to have two @ symbols" If the spec allows it it does. Although one could argue about the wtfness of the specs. – PeeHaa Sep 10 '12 at 16:47
  • And so it seems to be true, '"my@email"@sample.com' => string(21) ""my@email"@sample.com" – Jared Paolin Sep 10 '12 at 16:55
0

To check for a valid email adress I should use the filter_var function. It will validate the string and return true or false. You don't have to think about regular expressions.

Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55