Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    California
    Posts
    201
    Thanks
    30
    Thanked 8 Times in 8 Posts

    Cant get validation to work

    Hi all,

    I'm having a hard time trying to get this code to work. Any help would be greatly appreciated! I am trying to accomplish the follow with the code below:

    1. User enters web page (Start Free Trial | Learning Genie)
    2. User clicks Activate Account and they get an error that shows up in an empty div called .alert that says "Choose a product"
    3. When they choose from the drop down, that error should go away. This is when you see the Classroom Size select drop down show up
    4. User clicks button. If this is left at default than an error should show up in the .alert div and say "Please choose classroom size". Again, once they choose the classroom size, the error goes away (.hide()) NOTE: The classroom size select drop down is dynamically changing the buttons href.
    5. Then if the the user clicks Activate Account, the privacy policy error shows up.
    6. Once they click that checkbox that error should go away, and there are no errors, the user is then sent to the checkout page after button click

    Hopefully it makes sense. You can see all this at Start Free Trial | Learning Genie

    Thanks in advance!!


    Code:
    jQuery(document).ready(function() {
    	    jQuery(".alert").hide();
            jQuery('#option_selector').change(refreshInputs);
    		jQuery('#destination1, #destination2').change(changeLink);
    		jQuery('.engage').click(changeLink);
    		refreshInputs();
    		changeLink();
    		
    function refreshInputs() {
        var name = jQuery('#option_selector').attr('name');
        var val = jQuery('#option_selector').val();
        jQuery('#'+name+' div').hide();
        jQuery('#'+val).show();
    }
    
    function changeLink(e) { 
    		e.preventDefault();         
            
    		
    		if (!jQuery('input[type="checkbox"]').is(':checked')) {
    			jQuery('.engage').attr('href', jQuery(this).val())
    			jQuery('.alert').text('Please agree with our Privacy Policy and Trial Agreement').fadeIn();
            } else {
    			document.location.href = this.href;
    		} 		               		
    }
    });
    "The problem with troubleshooting is that trouble shoots back."

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    a very barebones example (assuming you can do the second select yourself):

    Code:
    <body>
    <select id="sel">
    <option>select one</option>
    <option>one</option>
    <option>two</option>
    <select>
    <input type="checkbox" id="box"/>
    <div id="warn"></div>
    <input type="button" value="activate" id="act"> 
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
      <script> 
      function clearIt() {
          $("#warn").text("");
      }
      
      $("#sel").on("change", clearIt);
      
      $("#box").on("change", clearIt);
      
      $("#act").on("click", function () {
          clearIt()
          if ($("#sel").prop("selectedIndex") == 0) {
              $("#warn").text("please select a product");
              return false;
          }
          if (!$("#box").prop("checked")) {
              $("#warn").text("please agree to conditions");
              return false
          }
          alert("activating account");
      });
    
     </script>
      </body>

  • Users who have thanked xelawho for this post:

    eberdome (03-16-2014)

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    California
    Posts
    201
    Thanks
    30
    Thanked 8 Times in 8 Posts
    Thank you xelawho! This definitely led me in the right direction. For you and others, this was my final solution!

    Code:
      jQuery(document).ready(function($) {
          $(".alert").hide();
          $('#option_selector').on("change", clearIt, hideDivs);
          $('#destination1, #destination2').on("change", makeLink);
          $("#agreement").on("change", clearIt);
          $("#engage").on("click", buttonClicked);
          clearIt();
          hideDivs();
    
          function clearIt() {
              $(".alert").hide();
          }
    
          function hideDivs() {
              var name = $('#option_selector').attr('name');
              var val = $('#option_selector').val();
              $('#' + name + ' div').hide();
              $('#' + val).show();
              clearIt();
          }
    
          function makeLink() {
              $('#engage').attr('href', $(this).val());
              clearIt();
          }
    
          function buttonClicked() {
              clearIt();
              if ($('#option_selector').prop('selectedIndex') == 0) {
                  $('.alert').text('Please choose a product').fadeIn();
                  return false;
              }
              if ($('#destination2').is(':hidden') && $('#destination1').prop('selectedIndex') == 0) {
                  $('.alert').text('Please choose classroom size').fadeIn();
                  return false;
              }
              if ($('#destination1').is(':hidden') && $('#destination2').prop('selectedIndex') == 0) {
                  $('.alert').text('Please choose classroom size').fadeIn();
                  return false;
              }
              if (!$('input[type="checkbox"]').is(':checked')) {
                  $('.alert').text('Please agree with our Privacy Policy and Trial Agreement').fadeIn();
                  return false;
              }
          }
      });
    "The problem with troubleshooting is that trouble shoots back."


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •