6

I have a multiselect checkbox form to which I add a JS to make sure the visitor selects at least one option

<div class="form-group options">
<label class="control-label col-md-4" for="optiontext">Specify an option</label>
<div class="col-md-6">
    <input type="checkbox" name="option[]" value="option1" required/> Option 1<br>
    <input type="checkbox" name="option[]" value="option2" required/> Option 2<br>
    <input type="checkbox" name="option[]" value="option3" required/> Option 3<br>
    <input type="checkbox" name="option[]" value="option4" required/> Option 4<br>
    <input type="checkbox" name="option[]" value="option5" required/> Option 5<br>
    <input type="checkbox" name="option[]" value="option6" required/> Option 6<br>
    <input type="checkbox" name="option[]" value="option7" required/>  Option 7
</div>

That's the JS

$(function(){
var requiredCheckboxes = $('.options :checkbox[required]');
requiredCheckboxes.change(function(){
    if(requiredCheckboxes.is(':checked')) {
        requiredCheckboxes.removeAttr('required');
    } else {
        requiredCheckboxes.attr('required', 'required');
    }
});

Now ALL IS WORKING fine, the only thing is that the error message that comes up is Please check this box if you want to proceed but I would like the message to be Please select at least one option to proceed like illustrated on the picture here.

enter image description here

How do I go about it?

FusionDesign
  • 353
  • 1
  • 4
  • 13
  • May I ask you when you click the view source what is the tag and the attributes associated with this message ? If no answer for this question can you tell me what a plugin is that you use here or the script name – Osama Apr 22 '17 at 21:03
  • I'm actually using Kohana framework for this project. – FusionDesign Apr 22 '17 at 21:16

2 Answers2

11

It's very simple to control custom messages with the help of HTML5 event oninvalid

Code:

<input type="checkbox" value="option1"
   oninvalid="this.setCustomValidity('Please select at least one option to proceed')">
Leo
  • 7,274
  • 5
  • 26
  • 48
  • L.Kelmendi That did the trick nicely, simple yet efficient solution [thumbs up] – FusionDesign Apr 22 '17 at 21:37
  • This works, but when I do not check the box and click the submit, then I check the box, it shows the error again. – keramat Mar 06 '22 at 05:14
  • 1
    setCustomValidity adds a property marking the field as invalid. You have to clear it yourself when the input is modified by adding something along the lines of `oninput="setCustomValidity('')"` From https://stackoverflow.com/questions/14043589/html5-oninvalid-doesnt-work-after-fixed-the-input-field – quickpocket Nov 12 '22 at 00:26
0

I implemented this as follows:

    <html>
    <head>
        <title>test</title>
    </head>
    <body>
        <form>
            <input type="checkbox" id="sterms" value="Agreed-to-Terms" onclick ="if (!this.checked) {this.setCustomValidity('برای ادامه لطفا این گزینه را تیک بزنید.');} else {this.setCustomValidity('');}" required>
                                <a href="privacy-policy.html">قوانین و شرایط استفاده را می پذیرم.</a>
            <button type="submit" class="form-control-submit-button">انجام آزمایش</button>
        </form>
        <script type="text/javascript">
            document.getElementById("sterms").setCustomValidity('برای ادامه لطفا این گزینه را تیک بزنید.');
        </script>
    </body>
</html>
keramat
  • 4,328
  • 6
  • 25
  • 38