Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #866606

    Hi I have created a form with a check box area and an email address in it. By default the check box area is ticked and I have selected Is Not Empty element validation for the check box.

    When the page loads the check box is ticked by default and for example if I untick the checkbox and type in a valid email address its still submitting the form.

    What I want to do is to stop the form from sending if the checkbox is unticked.

    I was just wondering if you could help me out.

    Kind regards

    #867012

    Hey mattlang,

    Could you please give us a link to your website, we need more context to be able to help you.

    Best regards,
    Victoria

    #867031
    This reply has been marked as private.
    #867460

    Hi,

    We can disable the submit button when the checkbox is unticked. Please add this script in the functions.php file.

    // custom script
    function add_custom_script(){
    ?>
    <script type="text/javascript">
    (function($){
    	$(document).ready(function() {
    		$("#top input[type='checkbox']").on('change', function(e) {
    			disableSubmit(this);
    		});
    
    		function disableSubmit(element) {
    			var submit = $('#top input[type="submit"]');
    		    if ($(element).is(':checked')) {
    				submit.removeAttr('disabled');
    			} else {
    				submit.attr('disabled', 'disabled');
    			}
    		}
    	});
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_script');

    Best regards,
    Ismael

    #867473
    This reply has been marked as private.
    #867964

    Hi,

    Glad that it is working. Please replace the script with the following code.

    // custom script
    function add_custom_script(){
    ?>
    <script type="text/javascript">
    (function($){
    	$(document).ready(function() {
    		function disableSubmit(element) {
    			var submit = $('#top input[type="submit"]');
    		    if ($(element).is(':checked')) {
    				submit.removeAttr('disabled');
    			} else {
    				submit.attr('disabled', 'disabled');
    			}
    		}
    
        $("#top input[type='checkbox']").on('change', function(e) {
    			disableSubmit(this);
    		});
    
        function addErrorNotif(element) {
          var form, error;
          form = $(element).parents('form');
          error = $(form).find('.error .is_email');
    
          $('<span class="error_notif">Please enter a valid email address!</span>').insertAfter(error);
    
          setTimeout(function() {
            $('.error_notif').remove();
          }, 3000);
        }
    
        $('#top input[type="submit"]').on('click', function() {
          setTimeout( $.proxy(function() {
            addErrorNotif(this);
          }, this), 1000);
        });
    	});
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_script');

    You can change the style of the error container by using the “error_notif” class selector.

    Best regards,
    Ismael

    #867971
    This reply has been marked as private.
    #868316

    Hi,

    Glad that Ismael helped you. Thanks for using Enfold :)

    Best regards,
    Nikko

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Check box is submitting form even when the not empty validation is enabled.’ is closed to new replies.