Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the Default Pop Up Message 2

Status
Not open for further replies.

berkshirea

Technical User
Mar 22, 2009
97
GB
Hi guys, I have the code below. It's working as I want it to be aside from the default popup message. Is it possible to change the message from "Please check this box if you want to proceed" to "Please select at least one box"

Thanks for any help.

Code:
<html><head>
    <style>
        
    </style>
 
</head>
<body>
    <script src="jquery.min.js"></script>
<form action="test.html">
  <input type="checkbox" name="whatever" value="1" required="" class="required_group">
  <input type="checkbox" name="whatever" value="2" required="" class="required_group">
  <input type="checkbox" name="whatever" value="3" required="" class="required_group">
  <input type="checkbox" name="whatever" value="4" required="" class="required_group">
  <input type="submit" name="submit">
</form>
    <script type="text/javascript">
        $('form').on('click', '.required_group', function() {
  $('input.required_group').prop('required', $('input.required_group:checked').length === 0);
});
    </script>

</body></html>
 
Cant see the code thats driving the pop up in what you shared, so can only assume its coming from the included JS file, if this is all you need it for you can / should rework the code to use your own rather than loading in JQuery.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Hi

Greg, there the [tt]input[/tt]'s [tt]required[/tt] attribute is toggled, so that is the browser's own message for unsatisfied required constraint.
See it in action on
As far as I know, such error message can not be changed, if you want custom message, you have to set up a whole new custom validator using [tt]HTMLObjectElement.setCustomValidity()[/tt]. But I have zero experience with that.


Feherke.
feherke.github.io
 
>Cant see the code thats driving the pop up

It's a default behaviour for HTML5


Probably simplest solution is to use HTML5's [tt]oninvalid[/tt] property to override the default

eg

Code:
<html><head>
    <style>
        
    </style>
 
</head>
<body>
    <script src="jquery.min.js"></script>
<form action="test.html">
  <input type="checkbox" name="whatever" value="1" required="" class="required_group" oninvalid="this.setCustomValidity('Please select at least one option to proceed')" >
  <input type="checkbox" name="whatever" value="2" required="" class="required_group" oninvalid="this.setCustomValidity('Please select at least one option to proceed')">
  <input type="checkbox" name="whatever" value="3" required="" class="required_group" oninvalid="this.setCustomValidity('Please select at least one option to proceed')">
  <input type="checkbox" name="whatever" value="4" required="" class="required_group" oninvalid="this.setCustomValidity('Please select at least one option to proceed')">
  <input type="submit" name="submit">
</form>
    <script type="text/javascript">
        $('form').on('click', '.required_group', function() {
  $('input.required_group').prop('required', $('input.required_group:checked').length === 0);
});
    </script>

</body></html>
 
Hi ggriffit, feherke & strongm,

I have tried the above on Brave, Firefox, Edge and Chrome but it doesn't seem to work. The pop-up message has changed but when I clicked a box (or even all of the boxes) the form won't submit and that same pop-up message appears.

 
Works fine on Edge here (well, mostly fine, due to the way HTML5 caches state)

It does require that jquery.min.js is available, which I assume is the case - although the symptoms you describe seem to indicate that it isn't loading.
 
Hi

Setting it [tt]oninvalid[/tt] does not seem to be enough because
HTMLObjectElement.setCustomValidity said:
It's vital to set the message to an empty string if there are no errors. As long as the error message is not null, the form will not pass validation and will not be submitted.

Unfortunately in absence of an onvalid event my best idea to clear is [tt]onclick[/tt] :
Code:
$[teal]([/teal][i][green]'input.required_group'[/green][/i][teal])[/teal]
    [teal].[/teal][COLOR=orange]on[/color][teal]([/teal][i][green]'invalid'[/green][/i][teal],[/teal] [b]function[/b][teal]() {[/teal] [b]this[/b][teal].[/teal][COLOR=orange]setCustomValidity[/color][teal]([/teal][i][green]'Please select at least one option to proceed'[/green][/i][teal]) })[/teal]
    [teal].[/teal][COLOR=orange]on[/color][teal]([/teal][i][green]'click'[/green][/i][teal],[/teal] [b]function[/b][teal]() {[/teal] $[teal]([/teal][i][green]'input.required_group'[/green][/i][teal]).[/teal][COLOR=orange]each[/color][teal]([/teal][b]function[/b][teal]() {[/teal] [b]this[/b][teal].[/teal][COLOR=orange]setCustomValidity[/color][teal]([/teal][i][green]''[/green][/i][teal]) }) });[/teal]
Which needs combined with the original code :

Feherke.
feherke.github.io
 
Hi feherke, that's AWESOME! it worked. Thank you to all of you guys. Have a great day!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top