Reset form invalid values |
Reset form invalid values |
RainLover |
Apr 10 2019, 02:57 AM
Post
#1
|
Advanced Member Group: Members Posts: 216 Joined: 16-November 09 Member No.: 10,346 |
Here’s a sample form:
CODE <form> <input type="number"> <input type="number"> <button type="button">Reset</button> </form> var form = document.querySelector('form'); function detectChange() { var inputs = form.querySelectorAll('input'); for (var input of inputs) { if (input.value) { return true; } } } form.querySelector('button').addEventListener('click', function() { if (detectChange() && confirm('Are you sure you want to reset?')) { form.reset(); } }); DEMO I’d like the reset button to work even if the user enters non-numeric values. |
Christian J |
Apr 10 2019, 12:54 PM
Post
#2
|
. Group: WDG Moderators Posts: 9,678 Joined: 10-August 06 Member No.: 7 |
The above applies to all Reset buttons, regardless of how they're made. Yes, exactly! Any workaround? I'd just use an HTML Reset button, if you need one. Otherwise, why check in the javascript that the number INPUT field has a value? Resetting an empty form field doesn't hurt anything, AFAIK. |
RainLover |
Apr 12 2019, 02:37 AM
Post
#3
|
Advanced Member Group: Members Posts: 216 Joined: 16-November 09 Member No.: 10,346 |
QUOTE The above applies to all Reset buttons, regardless of how they're made. The confirm dialog box prevents unwanted reset of the form. QUOTE Otherwise, why check in the javascript that the number INPUT field has a value? Resetting an empty form field doesn't hurt anything, AFAIK. The confirm box should appear only when there’s a value. (a non-empty input field) |
pandy |
Apr 12 2019, 08:46 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,735 Joined: 9-August 06 Member No.: 6 |
Some find confirm boxes irritating. That warning about reset buttons have been around since the beginning of things. I never thought it was a real concern. Today, when the average user is much more experienced I don't think it's a concern at all. But that's me.
|
Lo-Fi Version | Time is now: 1st June 2024 - 12:18 AM |