The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Reset form invalid values
RainLover
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
Christian J
post Apr 10 2019, 05:37 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,665
Joined: 10-August 06
Member No.: 7



QUOTE(RainLover @ Apr 10 2019, 09:57 AM) *

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

I recall form fields with invalid values are not passed on to javascript (or submitted). So my guess is that if a user enters a non-numeric value in a "type=number" form field, the field will appear to have no value and "if(input.value)" will not return true.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Apr 10 2019, 05:58 AM
Post #3


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,734
Joined: 9-August 06
Member No.: 6



I guess a normal reset button is out of the question?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
RainLover
post Apr 10 2019, 12:15 PM
Post #4


Advanced Member
****

Group: Members
Posts: 216
Joined: 16-November 09
Member No.: 10,346



QUOTE(pandy @ Apr 10 2019, 05:58 AM) *

I guess a normal reset button is out of the question?

QUOTE
You should usually avoid including reset buttons in your forms. They're rarely useful, and are instead more likely to frustrate users who click them by mistake (often while trying to click the submit button).

Source: MDN
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic
RainLover   Reset form invalid values   Apr 10 2019, 02:57 AM
CharlesEF   Is this actual code? The form HTML has several pr...   Apr 10 2019, 03:12 AM
RainLover   The function detectChange() doesn't return fa...   Apr 10 2019, 12:08 PM
CharlesEF   The function detectChange() doesn't return f...   Apr 10 2019, 03:49 PM
Christian J   Well, that function should return true or false...   Apr 10 2019, 04:58 PM
Christian J   I’d like the reset button to work even if the u...   Apr 10 2019, 05:37 AM
pandy   I guess a normal reset button is out of the questi...   Apr 10 2019, 05:58 AM
RainLover   I guess a normal reset button is out of the quest...   Apr 10 2019, 12:15 PM
pandy   I guess a normal reset button is out of the ques...   Apr 10 2019, 02:58 PM
RainLover   I recall form fields with invalid values are not ...   Apr 10 2019, 12:12 PM
Christian J   Source: [url=https://developer.mozilla.org/en-US/...   Apr 10 2019, 12:54 PM
RainLover   The confirm dialog box prevents unwanted reset o...   Apr 12 2019, 02:37 AM
pandy   Some find confirm boxes irritating. That warning a...   Apr 12 2019, 08:46 AM
Christian J   Some find confirm boxes irritating. Yes, Jakob d...   Apr 12 2019, 09:56 AM
RainLover   Some find confirm boxes irritating. They're ...   Apr 12 2019, 10:54 PM
CharlesEF   Actually, isn't the form that has the reset me...   Apr 10 2019, 05:56 PM
pandy   Yes. There is no such attribute. Unless HTML 5 has...   Apr 10 2019, 08:52 PM
Christian J   Yes. There is no such attribute. Unless HTML 5 ha...   Apr 11 2019, 04:58 AM
pandy   Doesn't anyone understand it anymore? There is...   Apr 11 2019, 06:21 AM
Christian J   Doesn't anyone understand it anymore? There i...   Apr 11 2019, 09:51 AM
pandy   A T T R I B U T E ! :lol: No, I meant simil...   Apr 11 2019, 10:19 AM
Christian J   Actually, isn't the form that has the reset m...   Apr 11 2019, 04:57 AM
CharlesEF   No, an HTML Reset button doesn't need to use...   Apr 11 2019, 12:33 PM
Christian J   This one makes the Reset button disabled until a f...   Apr 12 2019, 09:43 AM
RainLover   This one makes the Reset button disabled until a ...   Apr 12 2019, 10:14 AM
Christian J   What if the user clicks [i]Cancel? You're ri...   Apr 12 2019, 10:58 AM
RainLover   What if the user clicks [i]Cancel? You're r...   Apr 12 2019, 11:07 AM
Christian J   [code]if (detectChange() && ...   Apr 13 2019, 04:12 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 13th May 2024 - 07:02 AM