Code: Select all
if(preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,6})$/', $textbox)) {
echo "Valid!";
} else {
echo "Invalid!";
}Moderator: General Moderators
Code: Select all
if(preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,6})$/', $textbox)) {
echo "Valid!";
} else {
echo "Invalid!";
}Code: Select all
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?Code: Select all
filter_var($email, FILTER_VALIDATE_EMAIL);Code: Select all
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") ||
checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
Code: Select all
function validEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
list($name, $domain) = explode('@', $email);
if(getmxrr($domain)) {
return true;
}
}
return false;
}Wow, grumpy much? "thinly veiled slights", "flame others"? Seeing that you just got here, you must not have seen any of my other posts, so excuse me if I occasionally make a mildly sarcastic comment meant in the friendliest of ways. Hopefully Buckit didn't take this as a "slight" or "flame". You should self moderate the way you're going off half-cocked or risk alienating yourself from this resource.Bind wrote:AbraCadaver,
I did look through the code.
Filters were not present in php prior to version 5.2, so the script posted (that you are blasting and dismissing without even reading it) was a valid way to do it.
Additionally, there are many hosting enviroments still not updated to the current php version that filters can not be used on.
I do not think any of us here want to read your thinly vieled slights on code posted by others, who are only trying to assist others. I know I am not.
This is a resource for learning and helping others - not a place to flame others who are actively helping people.
Buckit,
Thank you for posting your code, as I am sure it will help many people who are using an older version of PHP that can't use filters.
Good to know, thanks for replying. After viewing your reply I looked back at my code and saw that it could be shortened (slightly):buckit wrote:no offense taken by me! I am using the latest version of PHP so I am now updating my function with the one you posted above!
and I learned something here... there is something called filters thats new(ish) to PHP! something for me to lookup and apply!
Everything I know about PHP (only started using it 8months ago) is from Google. I only recently started contributing to this forum because I noticed I knew some answers to questions or at least thought I could provide valid input to maybe help others out! Hopefully I am actually helping!
anyway! Thanks for your post AbraCadaver! I'll definitely be able to learn something from that function that I can apply elsewhere!
Code: Select all
function validEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
list($name, $domain) = explode('@', $email);
return getmxrr($domain);
}
return false;
}Code: Select all
'/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i'Thank you, I really appreciate that comprehensive list.MichaelR wrote:shawngoldw: it doesn't limit length, doesn't allow the full range of dot-atom text, doesn't allow a quoted string or obsolete local-part, doesn't allow domain literals, wrongly allows consecutive hyphens in the domain name, doesn't allow internationalized domain names, wrongly allows a domain label to begin and/or end with a hyphen, disallows some TLDs, and allows underscores in the domain name.
Code: Select all
/^(?!.{255,})(?!.{65,}@)(?:[a-z0-9_-]+(?:\.[a-z0-9_-]+)*)@(?:[a-z0-9]+(?:-[a-z0-9]+)*\.){1,}[a-z]{2,6}$/i