We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10357
    • 573 Posts
    Hi,

    is it possible to check that two fields match? i am looking to have a confirm email field.

    thanks,
    Gareth
      • 22840
      • 1,572 Posts
      Try something like this:-

      <form name = "myform">
      <p> Enter E-Mail Address: - <input type = "text" name = "emailadd" id = "emailadd" size = "42" onblur = "checkValidEmail()"> </p>
      <p> Confirm Email Address: - <input type = "text" name = "conemail" id = "conemail" size = "42" onblur = "checkConfirmEmail()"> </p>
      </form>
      
      <script type = "text/javascript">
      
      function checkValidEmail() {
      email1= document.myform.emailadd.value;
      if (/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email1)) {
      alert ("Valid Address"); // simply comment out this line if not required
      }
      else {
      alert ("Invalid Email Address - Please Re-Enter");
      document.myform.emailadd.value = "";
      document.myform.emailadd.focus();
      }
      }
      
      function checkConfirmEmail() {
      email2 = document.myform.conemail.value;
      if (email2 != email1) {
      alert ("Email addresses do not match - please re-enter!");
      document.myform.emailadd.value = "";
      document.myform.conemail.value = "";
      document.myform.emailadd.focus();
      }
      else { // delete these lines if alert not required.
      alert ("Passwords match!");
      }
      }
      
      </script>
        • 10357
        • 573 Posts
        thanks paul, looks great, but i was wondering if there is any one to do this using the inbuilt email validation messages.
          • 30223
          • 1,010 Posts
          You can do this on the server side by using eForm’s onValidate event. By the time that is triggered all field values are valid. It is then an easy task to simply compare the 2 fields and set an error if they don’t.

          //<?php
          function onValidateFunction(&$fields, &$vMsg, &$rMsg, &$rClass){
             
             if($fields['emailadd'] != $fields['emailcon']){
                //we have an error
                $vMsg['emailadd'] = "The two email addresses do not match!";
                $rClass['emailadd'] = $rClass['emailcon'] = 'invalid';
             }
             return true; //important
          }
            • 10357
            • 573 Posts
            thanks toby, works perfect grin
              • 3349
              • 126 Posts
              Realise its been a while since the last post but wondered if someone can explain how to implement this function onValidateFunction on the server side? And what the form code should look like

              Thanks
                • 18108
                • 24 Posts
                Hi Guys.

                Big slim:
                This is how I did it.
                My snippet call:

                [[OnBeforeMailSent]]
                [[eForm? &formid=`MyForm` &to=`[email protected]` &from=`[email protected]` &subject=`Newsletter Sign-up` &tpl=`Newsletter_Chunk` &report=`newsletterreport` &eFormOnValidate=`confirmeamils`]]

                The I created a snippet named confirmeemail with TobyL’s function (thank you toby!):

                function confirmeamils(&$fields, &$vMsg, &$rMsg, &$rClass){
                   
                   if($fields['email'] != $fields['email2']){
                      //we have an error
                      $vMsg['email'] = "The two email addresses do not match!";
                      $rClass['email'] = $rClass['email2'] = 'invalid';
                   }
                   return true; //important
                }
                


                Hope that will help you.

                Regards,

                Mamu

                  Thank you for Modx, and thank you for your help.