0

I want to add a checkbox with a required field.

My code is :

<div>
    <input class="input required-entry" type="checkbox" id="check" name="check" value="1" data-validate="{required:true, 'validate-one-required-by-name':true}" />  I agree to the Terms and Conditions.
</div>

But it's not working.

Thank you

0

1 Answer 1

0

You must have data-mage-init='{"validation":{}}' in your form

<form id="custom-form" data-mage-init='{"validation":{}}'>
    <input class="input required-entry" type="checkbox" id="check" name="check" value="1" data-validate="{required:true, 'validate-one-required-by-name':true}" />  I agree to the Terms and Conditions.
    <input type="submit" value="Submit">    
</form>

Or

<form id="custom-form">
    <input class="input required-entry" type="checkbox" id="check" name="check" value="1" data-validate="{required:true, 'validate-one-required-by-name':true}" />  I agree to the Terms and Conditions.
    <input type="submit" value="Submit">    
</form>

<script type="text/x-magento-init">
    {
        "#custom-form": {
            "validation": {}
        }
    }
</script>
8
  • Thank you, can we display alert?
    – gill
    Commented Sep 4, 2018 at 20:42
  • What do you mean by alert and where ?
    – PЯINCƎ
    Commented Sep 4, 2018 at 21:19
  • When I click on submit if checkbox is uncheck then i want to display alert ex plz agree terms and conditions.
    – gill
    Commented Sep 5, 2018 at 2:20
  • if(jQuery('#check').not(':checked').length){ alert('Not checked'); }
    – PЯINCƎ
    Commented Sep 5, 2018 at 7:46
  • I tried but this code is not working
    – gill
    Commented Sep 5, 2018 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.