Check email address format (PHP & Javascript)

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.

Verify email format with Javascript

Insert this code between <head> and </head> tags of your HTML document :

  1. <script language="javascript">
  2. function checkMail(email) {
  3.   var mail = email.value;
  4.   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');
  5.  
  6.   if(!reg.test(mail) || mail == "")
  7.   {
  8.     alert("Your email address isn't valid!");
  9.     return false;
  10.   }
  11.   else {
  12.       alert("Email address is okay, let's send the form!");    
  13.   }
  14. }
  15. </script>

Insert a form between <body> and </body> tags :

  1. <form id="form" name="form" method="post" action="" onsubmit="return checkMail(this.courriel)">
  2.   <label>Email address
  3.     <input type="text" name="email" id="email" />
  4.   </label>
  5.   <label>
  6.     <input type="submit" name="test" id="test" value="Test" />
  7.   </label>
  8. </form>

Eventonsubmit calls checkMail() function and gives him text value of email field. Email field is identified by ID email.

Try this script online

Verify email format with PHP

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.

  1. function validMail($addr) {
  2.   if ((!ereg(".+\@.+\..+", $addr)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $addr)))
  3.     RETURN FALSE;
  4.   else
  5.     RETURN TRUE;
  6. }
  7. // Use :
  8. if(!validMail('infoaddress.com')) {
  9.   echo "Your email address is invalid!";
  10. }

It won't prove real existence of the email address, but it will avoid the user from writing stupid stuff.

It's about

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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <actionscript>, <apache>, <bash>, <dos>, <html>, <javascript>, <mysql>, <php>, <xml>.
  • Every instance heading tags will be modified to include an id attribute for anchor linking.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Image links with 'rel="lightshow"' in the <a> tag will appear in a Lightbox slideshow when clicked on.
  • Links to HTML content with 'rel="lightframe"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to video content with 'rel="lightvideo"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to inline or modal content with 'rel="lightmodal"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • Insert <!--tableofcontents [list: ol; title: Table of Contents; minlevel: 2; maxlevel: 3; attachments: yes;]--> to insert a mediawiki style collapsible table of contents. Arguments within [] are optional.

More information about formatting options