Here are two code snippets to verify email addresses, which is quite useful when you wish to validate a form with Javascript or PHP, avoiding users to write anything as email.
Insert this code between <head> and </head> tags of your HTML document :
<script language="javascript">
function checkMail(email) {
var mail = email.value;
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
if(!reg.test(mail) || mail == "")
{
alert("Your email address isn't valid!");
return false;
}
else {
alert("Email address is okay, let's send the form!");
}
}
</script>
Insert a form between <body> and </body> tags :
<form id="form" name="form" method="post" action="" onsubmit="return checkMail(this.courriel)">
<input type="text" name="email" id="email" />
<input type="submit" name="test" id="test" value="Test" />
Eventonsubmit calls checkMail() function and gives him text value of email field. Email field is identified by ID email.
Try this script online
This little PHP function allows you to check if the user wrote a valid email address that may exist. You call this function when you're checking POST values after form submission.
function validMail($addr) {
if ((!ereg(".+\@.+\..+", $addr)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $addr)))
RETURN FALSE;
else
RETURN TRUE;
}
// Use :
if(!validMail('infoaddress.com')) {
echo "Your email address is invalid!";
}
It won't prove real existence of the email address, but it will avoid the user from writing stupid stuff.
Comments
thanks a lot! It's really
thanks a lot! It's really help me
it runs two php scripts when
it runs two php scripts when followed first adds the deny from IP to the .ht access that's in the root below all directories {except the directory containing the error pages and php scripts for blacklisting
Thank you for writing such a
Thank you for writing such a beautiful articles. it's really informative for me
bitte schön
bitte schön
Thanks for the Code , I was
Thanks for the Code , I was searching it and it worked in the way I want
Post new comment