Change required field message

Asked

Viewed 270 times

0

How to change the message below and stay with the simplicity and coherence I need?

inserir a descrição da imagem aqui

I have the following code:

$(function() {
		var requiredCheckboxes = $('.periodicidade :checkbox[required]');
		requiredCheckboxes.change(function() {
			if (requiredCheckboxes.is(':checked')) {
				requiredCheckboxes.removeAttr('required');
			}
			else {
				requiredCheckboxes.attr('required', 'required');
			}
		});
});
<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>

Remarks:

  1. Mandatory marking of at least 1 (a) checkbox
  2. Utilise setCustomValidity for the modification of the message

1 answer

0


Good afternoon gentlemen,

[SOLVED] Below is the solution available in another forum:

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');
  }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.