Monday, June 16, 2008

Email Address Verification

Using PHP:

function validate_email($email)
{
// - check for a invalid/false email address
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
// it failed the simple format test, so return with an invalid address error
return 1;
} else {
return 0; // send mail
}
}

// calls the email that was provided and parses it in the validate function
$valid = validate_email($email);

switch ($valid)
{
case 0:
//send the mail
mail ($email,"Subject",$message,$headers ) ;
case 1:
// show some error message
}

Using Javascript:

var result;

function checkemail()
{
var str=document.mailcheck.emailaddress.value;
var filter=/^.+@.+\..{2,3}$/;

if (filter.test(str))
result=true;
else
{
alert("Please input a valid email address!");
result=false;
}
return (result);
}

function checkall()
{
if (document.layers||document.all)
return checkemail();
else
return true;
}





No comments: