Reputation: 77
I have the following regex expression
/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
that currently matches email addresses of the form [email protected] (do not want this email address to be accepted bc it starts with a hyphen) If I want it to reject email addresses that start with a - (hyphen) and only accept those that start with an underscore _ how would I modify the regex?
Another question before modifying the regex -- can emails even start with hyphens? If they can, then there's no point in modifying the regex.
Thanks.
Upvotes: 0
Views: 871
Reputation: 7035
[email protected]
is a valid email address.
From RFC3696 Section 3: Restrictions on email addresses:
local-parts may consist of any combination of alphabetic characters, digits, or any of the special characters
! # $ % & ' * + - / = ? ^ _ ` . { | } ~
It may be interesting to note also that "[periods] may not be used to start or end the local part, nor may two or more consecutive periods appear."
Upvotes: 2
Reputation: 145482
To find out if email addresses with leading -
hyphens are allowed, you could use a more standards-compliant regex to check:
print filter_var("[email protected]", FILTER_VALIDATE_EMAIL);
Upvotes: 2
Reputation: 146302
Just fix the 1st part of your regex:
/^[_a-zA-Z0-9]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
Upvotes: 0
Reputation: 1129
/^[_a-zA-Z0-9]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
Upvotes: 2