Valider le format d'un courriel (PHP & Javascript)

Voici deux échantillons de script pour valider des adresses courriel, ce qui est communément utilisé dans la validation de formulaires, pour éviter que les utilisateur.

Valider le format d'un courriel en Javascript

Insérez ce petit bout de Javascript entre les balises <head> et </head> de votre document HTML :

  1. <script language="javascript">
  2. function checkMail(courriel) {
  3.   var mail = courriel.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("Votre adresse courriel est invalide, veuillez la vérifier. "+mail);
  9.     return false;
  10.   }
  11.   else {
  12.       alert("Adresse valide, on envoie le formulaire!");       
  13.   }
  14. }
  15. </script>

Insérer un formulaire de ce genre entre les balises <body> et </body> :

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

L'événement onsubmit appelle la fonction checkMail() et lui transmet la valeur du champs texte courriel. Le champs courriel est identifié par le ID courriel.

Essayez le script en ligne

Valider le format d'un courriel en PHP

Voici une petite fonction PHP qui permet de vérifier que l'utilisateur a bel et bien entré une adresse courriel qui puisse exister.

  1. function validMail($addr) {
  2.   if ((!ereg(".+\@.+\..+", $addr)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $addr)))
  3.     RETURN FALSE;
  4.   else
  5.     RETURN TRUE;
  6. }
  7. // Exemple d'utilisation :
  8. if(!validMail('infoadresse.com')) {
  9.   echo "Votre adresse est invalide!";
  10. }

Ça ne teste pas son existence, mais ça évite que l'utilisateur instinctivement des conneries.

Ça parle de

Commentaires

Commenter sur ce sujet :

Le contenu de ce champ sera maintenu privé et ne sera pas affiché publiquement.
  • Les adresses de pages web et de messagerie électronique sont transformées en liens automatiquement.
  • Les lignes et les paragraphes vont à la ligne automatiquement.
  • 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.

Plus d'informations sur les options de formatage