We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8822
    • 203 Posts
    I am using the excellent tutorial: http://numberfiveone.com/modx-tutorial-formit-with-jquery-validation to validate my Revo 2.2.6 formit form with jquery. It is all working great, but in addition to highlighting the required field in red, i also want to display a message underneath the required field (or at the beginning/end), telling the user that these are required fields. Not being a jquery expert, is anyone able to help me?

    JS code:
    $(document).ready(function () {
    
        $("form#buynowform").submit(function () {
            //When the form is submitted
            var elem = $("form#buynowform").children('fieldset').children('input, textarea');
            var error, foc;
    
            //Loop through each input and textarea
            elem.each(function (index) {
                
                //Does this have the class "required"?
                if ($(this).hasClass('required') == true) {
    
                    //It has the class, is it empty or still have the default value?
                    if (!this.value || this.value == this.defaultValue) {
    
                        //Add the error class for CSS styling
                        $(this).addClass(" errorbuynow");
    
                        //Switch the error var to true
                        error = true;
    
                        //If this is the first required element not filled out, save the ID
                        if (!foc) foc = $(this).attr("id");
                    } else {
    
                        //If this is filled out make sure it doesn't have the error class
                        $(this).removeClass("errorbuynow");
                    }
                }
            });
    
            //If error has been switched to true
            if (error) {
    
                //Focus on the first required element that hasn't been filled out.
                if (foc) $('#' + foc).css("color", "#000").css("fontStyle", "normal").focus();
    
                //Stop the form from submitting
                return false;
            } else {
                //Clear default values on non required elements before submit continues
                if (elem.value == this.defaultValue)
                    this.value = "";
            }
        });
    
    	    //Remove Default Text On Focus
        $("input, textarea").focus(function() { 
            if( this.value == this.defaultValue ) { 
                this.value = ""; 
                $(this).css("color","#000").css("fontStyle","normal"); 
            } 
        }).blur(function() { 
            if( !this.value.length ) {               
                $(this).css("color","#999").css("fontStyle","italic"); 
                this.value = this.defaultValue; 
            } 
        }); 
    });
    


    Thanks smiley

    • Hiya Emily.

      this should work.

      $(document).ready(function () {
       
          $("form#buynowform").submit(function () {
              //When the form is submitted
              var elem = $("form#buynowform").children('fieldset').children('input, textarea');
              var error, foc;
       
              //Loop through each input and textarea
              elem.each(function (index) {
                   
                  //Does this have the class "required"?
                  if ($(this).hasClass('required') == true) {
       
                      //It has the class, is it empty or still have the default value?
                      if (!this.value || this.value == this.defaultValue) {
       
                          //Add the error class for CSS styling
                          $(this).addClass(" errorbuynow");
                          $(this).append('<p>This field is required.</p>');
       
                          //Switch the error var to true
                          error = true;
       
                          //If this is the first required element not filled out, save the ID
                          if (!foc) foc = $(this).attr("id");
                      } else {
       
                          //If this is filled out make sure it doesn't have the error class
                          $(this).removeClass("errorbuynow");
                      }
                  }
              });









      Quote from: Emily at Mar 05, 2013, 12:29 PM
      I am using the excellent tutorial: http://numberfiveone.com/modx-tutorial-formit-with-jquery-validation to validate my Revo 2.2.6 formit form with jquery. It is all working great, but in addition to highlighting the required field in red, i also want to display a message underneath the required field (or at the beginning/end), telling the user that these are required fields. Not being a jquery expert, is anyone able to help me?

      JS code:
      $(document).ready(function () {
      
          $("form#buynowform").submit(function () {
              //When the form is submitted
              var elem = $("form#buynowform").children('fieldset').children('input, textarea');
              var error, foc;
      
              //Loop through each input and textarea
              elem.each(function (index) {
                  
                  //Does this have the class "required"?
                  if ($(this).hasClass('required') == true) {
      
                      //It has the class, is it empty or still have the default value?
                      if (!this.value || this.value == this.defaultValue) {
      
                          //Add the error class for CSS styling
                          $(this).addClass(" errorbuynow");
      
                          //Switch the error var to true
                          error = true;
      
                          //If this is the first required element not filled out, save the ID
                          if (!foc) foc = $(this).attr("id");
                      } else {
      
                          //If this is filled out make sure it doesn't have the error class
                          $(this).removeClass("errorbuynow");
                      }
                  }
              });
      
              //If error has been switched to true
              if (error) {
      
                  //Focus on the first required element that hasn't been filled out.
                  if (foc) $('#' + foc).css("color", "#000").css("fontStyle", "normal").focus();
      
                  //Stop the form from submitting
                  return false;
              } else {
                  //Clear default values on non required elements before submit continues
                  if (elem.value == this.defaultValue)
                      this.value = "";
              }
          });
      
      	    //Remove Default Text On Focus
          $("input, textarea").focus(function() { 
              if( this.value == this.defaultValue ) { 
                  this.value = ""; 
                  $(this).css("color","#000").css("fontStyle","normal"); 
              } 
          }).blur(function() { 
              if( !this.value.length ) {               
                  $(this).css("color","#999").css("fontStyle","italic"); 
                  this.value = this.defaultValue; 
              } 
          }); 
      });
      


      Thanks smiley

        ________

        Anne
        Toad-in-Chief
        Red Toad Media - Web Design, Louisville KY
        Hear me tweet: http://www.twitter.com/redtoadmedia
        "Bring on the imperialistic condiments." - Rory Gilmore
        • 8822
        • 203 Posts
        Hi, thanks for that, and for your quick response.

        Updated as you suggested - is there something specific that i am supposed to do to the css? On validation nothing changes and no error messages are displayed. If i add in color and font-size to the buynowerror class in my css then the input fields on my form all move position implying that the js is placing the text 'This field is required' alongside the field in question.....there is no text displayed though, even if the color i specify isn't white...... Any ideas?
        • Do you have a live link I can look at?

          Try replacing this:

          
          $(this).append('<p>This field is required.</p>');
          
          



          With this:

          $(this).after('<p>This field is required.</p>');
          


          I think that will accomplish what you want to achieve.

          Also make sure the errorbuynow class has display:block otherwise the message will never be seen.

          Hope that helps.
            Benjamin Marte
            Interactive Media Developer
            Follow Me on Twitter | Visit my site | Learn MODX
            • 8822
            • 203 Posts
            That worked perfectly - thanks smiley

            How do i clear the error message though in the js - if i then enter the required text, the text 'This field is required' is still displayed on validation when it should be removed. It also keeps adding the text 'This field is required' every time it is validated, regardless of whether it is already there?

            Thanks for your help, really appreciate it smiley
            • Emily, change this line of code:

              $(this).removeClass("errorbuynow");
              


              To this:

              $(this).removeClass("errorbuynow");
              $(this).next().remove();
              


              That should solve your problem, again when possible post a link we can check, it will be much easier to debug.

              Good luck.
                Benjamin Marte
                Interactive Media Developer
                Follow Me on Twitter | Visit my site | Learn MODX
                • 8822
                • 203 Posts
                Hi Ben,

                Thanks for that - the change you suggested fixed the first instance, i.e. the first field that had the text 'This field is required' - if all fields had an error message though it keeps adding the same validation error message......

                Thanks for your help [ed. note: Emily last edited this post 11 years, 1 month ago.]
                • Emily,

                  Try changing this:

                  $(this).after('<p>This field is required.</p>');
                  


                  To this:

                  
                  if(!$(this).next().hasClass('buynowerrormsg')){
                  
                  $(this).after('<p class="buynowerrormsg">This field is required.</p>');
                  
                  }else{
                  
                  $('.buynowerrormsg').show();
                  
                  }
                  
                  


                  To hide the error message change this:

                  $(this).next().remove();
                  


                  To this:

                  $(this).next('.buynowerrormsg').hide();
                  


                  If I did it right it should take care of all your issues.

                  Good luck.
                    Benjamin Marte
                    Interactive Media Developer
                    Follow Me on Twitter | Visit my site | Learn MODX
                    • 8822
                    • 203 Posts
                    Hi Ben, thanks - making good progress....

                    It now doesn't display the error message multiple times, which is great.

                    However, if i change to
                    $(this).next('.buynowerrormsg').hide();
                    it does not remove the error message when the field has been correctly filled in. If i leave the code as
                    	$(this).next().remove();
                    it does remove the error message correctly, but each time the 'buy button' button is clicked some of the labels disappear...........infact if you select the buy now button 3 times the button itself disappears (i have left it in that state now so that you can see what i am talking about!).

                    Any ideas?
                      • 8822
                      • 203 Posts
                      Looks like if i use
                      $(this).next('.buynowerrormsg').remove();
                      however it does work as i expected!
                      Thanks for your help, think i have got it !