Forum Moderators: coopster

Message Too Old, No Replies

Email validation.

         

Flolondon

12:00 pm on May 3, 2004 (gmt 0)

10+ Year Member



Please tell me what i have done wrong, it does not appear to show well... via the browser..

<?php
function ValidateMail($Email) {
global $HTTP_HOST;
$result = array(); if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {

$result[0]=false;
$result[1]="$Email is not properly formatted";
return $result;
}
list ( $Username, $Domain ) = split ("@",$Email);

if (getmxrr($Domain, $MXHost)) {

$ConnectAddress = $MXHost[0];
} else {

$ConnectAddress = $Domain;

} $Connect = fsockopen ( $ConnectAddress, 25 );

if ($Connect) {

if (ereg("^220", $Out = fgets($Connect, 1024))) {

fputs ($Connect, "HELO $HTTP_HOST\r\n");
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
$From = fgets ( $Connect, 1024 );
fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
$To = fgets ($Connect, 1024);
fputs ($Connect, "QUIT\r\n");
fclose($Connect);
if (!ereg ("^250", $From) ¦¦
!ereg ( "^250", $To )) {
$result[0]=false;
$result[1]="Server rejected address";
return $result;

} $result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of function
?>
} else {

$result[0] = false;
$result[1] = "No response from server";
return $result;
}

} else {

$result[0]=false;
$result[1]="Can not connect E-Mail server.";
return $result;
}


</form>
<p align="center">&nbsp; </p>
</div>
<p>&nbsp;</p>
<p align="center">&nbsp; </p>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

Flolondon

1:31 pm on May 3, 2004 (gmt 0)

10+ Year Member



or please check this one too.function checkEmail($email) {

if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
return FALSE;
}

list($Username, $Domain) = split("@",$email);

if(getmxrr($Domain, $MXHost)) {
return TRUE;
}
else {
if(fsockopen($Domain, 25, $errno, $errstr, 30)) {
return TRUE;
}
else {
return FALSE;
}
{
{
if(checkEmail($_POST['email']) == FALSE) {
echo "E-mail entered is not valid.";
} else {
echo "Success - please to to your email and click on the link to registrar!.";
}

woldie

4:28 pm on May 3, 2004 (gmt 0)

10+ Year Member



Hi M8,

Check this function, this works well for me...

function IsEmailInvalid($val)
{
$pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";

// match?
if (preg_match($pattern,$val))
{
return 0;
}
else
{
return 1;
}
}

Hope this helps...

httpwebwitch

8:03 pm on May 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Everyone has their favourite e-mail regex.

As a general rule, it's better to be liberal in allowing too much through than conservative and not allow someone to enter what really IS a valid email address. In simple form validation you can't check if an address is Real, so you're really just trying to catch obvious typos or bad input like "I'm not telling"

I use:
^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$

it allows domain addresses, but also understands IP addresses and multi-dot domains.

these are all vaild:
myname@mydomain.com
myname@mydomain.sub.ir.co.uk
myname@122.89.4.110
my.first.and.last.name@mydomain.site

Granted my pattern could be improved since it does allow some invalid patterns to get through, but it's good enough for most situations.

httpwebwitch

8:05 pm on May 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, forgot to mention, that's an EREG, not a PREG.

And since you're getting a true or false value back from the pattern matching, why not just return that from your function? IFs just clutter things up.


function isemail($var) {
return ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$", $var);
}

Flolondon

5:32 am on May 8, 2004 (gmt 0)

10+ Year Member



ok, thanks for your advice