ok first off let me say.. This NOT my code, I messed with it a bit, but I honestly forgot where I got it, I just googled it from somewhere, but it works GREAT. use it to check if an email is in the right format.
Function:
Use like this:Code:function check_email_address($email) {
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
return false;
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
Again.. not my code, but I thought you could use it!Code:if(!check_email_address($email)) {
//code to use for incorrect email
} else {
//code to use for correct email
}
Checkout my new forum! http://adminreference.com/
Nice thanks.. I will actually use this.. I was going to try and make something like this for my register script.. but i gave up on it.. So this can come in handy![]()
its awesome dude, its a great script, havent had a problem with it yet
Checkout my new forum! http://adminreference.com/
There are two alternatives here: PHP: Validate Email Address
One from Jordan and one from myself. The more the merrier![]()
Can any body tell me right e-mail format. In phpforfun code, I see {1,64} before @ and {1,255} after @. So in databases e-mail column must be size of 64+1+255?
I am using this function (and think its ok)Code:function test_email($email) {
$match = "/"
. "^[a-z0-9_-]+"
. "(\.[a-z0-9_-]+)*"
. "@"
. "[a-z0-9][a-z0-9-]*"
. "(\.[a-z0-9-]+)*"
. "\.([a-z]{2,6})$"
. "/i";
return preg_match($match, $email) ? true : false;
}
Correct. According to the RFC a valid email address contains a local-part@domain. As defined by the RFC, the local-part must be a maximum of 64 characters. The domain, defined in another section of the RFC says that a domain may contain labels between 1 and 63 characters inclusive. When all labels and dots are combined, it may not exceed 255 characters. Which suggests an email address can be of the form a@b.c.d.e
That said, no TLD that I know of (com, net, org) exceeds 6 characters (.museum is the largest I know). So allowing the TLD would be poor validation in my book.
I've been looking for something like this, so thanks.
One question though, my understanding is that when ICANN allows custom TLDs we're going to see email addresses such as drink@coke (with NO .com on the end)
Am I right in assuming that this will break pretty much every email validation script currently in use?
It is better to use PHP built in filters for this...
Not only can they process this much faster, they have been built off of experience and standards, not to mention they have been tested by many programmers.
To build your own is not only a waste of code and time... you are re-inventing the wheel.
Here's an example of the email filter.
I would recommend that you learn these filters, they can save you a lot of time coding.Code:if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'passed'; } else { echo 'failed'; }
Of course, this info is always available in the PHP: filter_var - Manual![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks