0

enter image description here

I have added the checkbox to my sign-up page. but I want that every user should check the button to create an account. I have tried to make it with Javascript. I am using Magento 2.3.0 Here is the code.

<div style="padding-top: 10px;">
    <input type="checkbox" required id="checkBoxRequired">
        <a href="#">Privacy & Policy</a>
    </input>
    <span id="span"></span>
</div>

function validateForm() {
    var checkbox = document.getElementById("checkBoxRequired");
    if (checkbox.checked == false) {
        document.getElementById("span").innerHTML = "*Checking the checkbox";
        document.getElementById("span").style.color = "red";
        document.getElementById("span").style.display = "block";
        return false;
    }
}

I have added onsubmit="return validateForm()" inside begining form tag!

2

you just need to overwrite register.phtml file in your theme

app/design/frontend/Vendor/Theme/Magento_Customer/form/register.phtml

and add the code below after confirmarion password field

<div class="field choice privacy_policy">
    <input type="checkbox" name="privacy_policy" title="<?= $block->escapeHtmlAttr(__('Privacy & Policy')) ?>" value="1" id="privacy_policy"<?php if ($block->getFormData()->getPrivacyPolicy()) : ?> checked="checked"<?php endif; ?> class="checkbox" data-validate="{required:true}">
    <label for="privacy_policy" class="label"><span><?= $block->escapeHtml(__('Privacy & Policy')) ?></span></label>
</div>

enter image description here You don't need to add any custom JavaScript validation

Hope this will help you!

1
  • The classes you added, is that mandatory or I can add as I want. Nov 12 '19 at 6:25

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.