CMS Form Validation

Joy Basak asked on October 31, 2017 10:28

I have one issue regarding form validation. Let me explain the scenario .I have a widget having multiple select checkbox, my requirement is to restrict user to check not more than 6 check boxes. Here Is my screen-cast https://www.screencast.com/t/IMCovFqGicr for better reference. I have tried with writing macros like {% PlacesOptions.Split('|').Count() < 7 %} ,but no luck

Correct Answer

Joy Basak answered on November 1, 2017 06:33

Hello all , Thanks for the reply. I tried with all the aforementioned solution but unfortunately none of them seems to be working for me . So i got the solution by the following piece of code :- count=0; foreach(x in PlacesOptions.Value.Split("|")) {count += 1;} return count < 7;

Thanks

0 votesVote for this answer Unmark Correct answer

Recent Answers


Matt Nield answered on October 31, 2017 12:22 (last edited on October 31, 2017 14:42)

If you add a regular expression validation to your field, you can use something like this ^([^\|]*|([^\|]*\|[^\|]*){0,6})$ to ensure no more than 7 options are entered.

The numbers at the end of the expression give you the minimum and maximum occurences.

An example of how I set it is below. I wanted - in this case to have no more than three options, so my numbers are {0,2}:

Usage example

4 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on October 31, 2017 14:32

You may use something as simple as this and these sort of things are better handled on the UI side rather than through Macros.

document.querySelectorAll('input[type="checkbox"]:checked').length < 7

0 votesVote for this answer Mark as a Correct answer

Matt Nield answered on October 31, 2017 14:37 (last edited on October 31, 2017 14:52)

Hi Joy, I've updated my answer above with a better regular expression that will work in the scenario that you explained.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on November 1, 2017 09:35

That's great, however the code i shared with you is from one of my project and it works. Most likely it could be some JavaScript error or code not getting called problem. Best Chetan

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.