templates/contact/includes/_contact.js.twig line 1

Open in your IDE?
  1. <script>
  2.     $(document).ready(function() {
  3.         $('#contact-form .submit-btn').on('click', function(ev) {
  4.             ev.preventDefault();
  5.             let errors = [];
  6.             let valid = true;
  7.             const nameValid = $('#contact_name').val() != null && $('#contact_name').val().length > 0;
  8.             if (nameValid == false) {
  9.                 valid = false;
  10.                 errors.push('Nom* obligatoire.');
  11.             }
  12.             const emailLengthValid = $('#contact_email').val() != null && $('#contact_email').val().length > 0;
  13.             if (emailLengthValid == false) {
  14.                 valid = false;
  15.                 errors.push('Email* obligatoire.');
  16.             }
  17.             const messageValid = $('#contact_message').val() != null && $('#contact_message').val().length > 0;
  18.             if (messageValid == false) {
  19.                 valid = false;
  20.                 errors.push('Message* obligatoire.');
  21.             }
  22.             const isEmail = isEmailFormat($('#contact_email').val());
  23.             if (isEmail == false) {
  24.                 valid = false;
  25.                 errors.push('Email: format invalide.');
  26.             }
  27.             const validPrivacy = $('#clone_contact_privacyPolicy').prop('checked');
  28.             if (validPrivacy == false) {
  29.                 valid = false;
  30.                 errors.push('Accépter la politique de confidentialité.');
  31.             }
  32.             const validRecaptcha = validateRecaptcha();
  33.             if (validRecaptcha == false) {
  34.                 valid = false;
  35.                 errors.push('reCaptcha invalide.');
  36.             }
  37.             if (valid == true) {
  38.                 $('#contact-form').submit();
  39.             } else {
  40.                 let htmlError = '<div class="alert alert-danger">Veuillez vérifier ces informations s\'il vous plaît:<ul>';
  41.                 
  42.                 errors.forEach((message, key) => {
  43.                     htmlError += '<li>' + message + '</li>';
  44.                 });
  45.                 htmlError += '</ul></div>';
  46.                 
  47.                 $('.alert-container').html(htmlError);
  48.             }
  49.         });
  50.         $('#clone_contact_privacyPolicy').on('click', function(ev) {
  51.             const checked = $(this).prop('checked');
  52.             $('#contact_privacyPolicy').prop('checked', checked);
  53.         });
  54.     });
  55.     /**
  56.      * @return {boolean} valid
  57.      */
  58.     function validateRecaptcha() {
  59.         var response = grecaptcha.getResponse();
  60.         if (response.length === 0) {
  61.             console.warn("reCaptcha not validated");
  62.             return false;
  63.         } else {
  64.             console.log("reCaptcha validated");
  65.             return true;
  66.         }
  67.     }
  68.     /**
  69.      * @param {string} text
  70.      * @return {boolean} isEmail
  71.      */
  72.     function isEmailFormat(text) {
  73.         var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  74.         return regex.test(text);
  75.     }
  76. </script>