Forum Moderators: open

Message Too Old, No Replies

Reset form invalid values

         

Rain_Lover

7:55 am on Apr 10, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month



Here’s a sample form:

<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 [jsfiddle.net]

I’d like the reset button to work even if the user enters non-numeric values.

NickMNS

12:17 pm on Apr 10, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Users can't enter non-numeric values because you have set your inputs as type=number. If you want non-numeric value you would need to change it to type=text.

Rain_Lover

5:16 pm on Apr 10, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month



Actually you can do it even in Chrome: copy and paste some text into the input fields.