Change required field message

Good night young nerds,

"Changing the message below and stay with the simplicity and consistency i need?"

inserir a descrição da imagem aqui

I have the following code:


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

HTML:

<div class="periodicidade">
<input type="checkbox" required value=1>Segunda-feira
<input type="checkbox" required value=2>Terça-feira
<input type="checkbox" required value=3>Quarta-feira
<input type="checkbox" required value=4>Quinta-feira
<input type="checkbox" required value=5>Sexta-feira
<input type="checkbox" required value=6>Sábado
<input type="checkbox" required value=7>Domingo
</div>

Comments:

  1. Mandatory marking at least one (1) checkbox
  2. Use setCustomValidity to change the message
requiredCheckboxes.get(0).setCustomValidity('Mark at least one checkbox');
1 Like

Wonderful megazoid! However, below is the correct way:

or simply:

var requiredCheckboxes = $('.periodicidade :checkbox[required]');

requiredCheckboxes.get(0).setCustomValidity('Mark at least one checkbox');

requiredCheckboxes.change(function() {
  if (requiredCheckboxes.is(':checked')) {
  requiredCheckboxes.get(0).setCustomValidity('');
    requiredCheckboxes.removeAttr('required');
  } else {
  requiredCheckboxes.get(0).setCustomValidity('Mark at least one checkbox');
    requiredCheckboxes.attr('required', 'required');
  }
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.