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

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>
  • Thank you, can we display alert? – gill Sep 4 '18 at 20:42
  • What do you mean by alert and where ? – PЯINCƏ Sep 4 '18 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 Sep 5 '18 at 2:20
  • if(jQuery('#check').not(':checked').length){ alert('Not checked'); } – PЯINCƏ Sep 5 '18 at 7:46
  • I tried but this code is not working – gill Sep 5 '18 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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