How to make one of two or more blocks mandatory

Modified on Mon, 27 May 2019 at 12:20 PM

This can be accomplished using JavaScript.

Example: A Blocks file with two blocks ‘Phone’ and ‘Mobile’. At least one of them must not be empty.

Add the following code to Main layout (Bootstrap)

<script>
function _initPreview() {
if ($("[data-blockname='Phone']").val().trim()=="" & $("[data-blockname='Mobile']").val().trim()=="")
{
alert ("Phone and Mobile Cannot both be Empty!");
return false;
}
return true;
}
</script>

_initPreview is a special function that is called first when the Preview button is clicked. The above code gets the values entered for Phone and Mobile blocks and returns false if both are empty otherwise returns true. It is important to return a value for the preview process to stop or continue.

The block fields are selected using the data-blockname attribute, the value of which is the actual name of the block.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article