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,665 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) |
Lo-Fi Version | Time is now: 16th May 2024 - 11:46 AM |