Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is disabling form submission affecting min and max attributes?

I'm doing some validation on a HTML form using min and max attributes. I also want to prevent form submission if the submit button is clicked once to prevent multiple form submissions before a page is reloaded.

Here's the inline code for disabling the button:

<input type="submit" name="name" value="REGISTER" onclick="this.disabled=true;this.form.submit();">

Here's the number input field code:

<input type="number" name="endNo" placeholder="END" min="5" max="20">

If I include the inline JS for the submit button, the min and max validation doesn't work. When removed, it seems to work just fine. Why is this happening and how can I satisfy both conditions, disable the button once clicked while still validating the min and max values for the number input field?

I'm using google chrome.

like image 483
andromeda Avatar asked Oct 17 '16 01:10

andromeda


People also ask

What is min and max attribute?

For the <meter> element, the min attribute defines the lower numeric bound of the measured range. This must be less than the minimum value ( max attribute), if specified. In both cases, if omitted, the value defaults to 1.

How do I disable a form in HTML?

Disabling all form elements HTML form elements have an attribute called disabled that can be set using javascript. If you are setting it in HTML you can use disabled="disabled" but if you are using javascript you can simply set the property to true or false .

Which attribute is not a part of the input tag?

Which of the following is not a type of attribute for input tag? Explanation: Day is not defined in the pre-defined attribute list of input tag. Week attribute defines week and year when used as attribute in input tag. Month specifies month and year when it is accessed in input tag.


1 Answers

The submit method on form element does what it promises, it just submits the form, doesn't do a validation. You can manually do validation with the checkValidity method.

<input type="submit" name="name" value="REGISTER" 
    onclick="if(this.form.checkValidity()){this.disabled=true;this.form.submit();}">
like image 195
Gokhan Kurt Avatar answered Sep 21 '22 19:09

Gokhan Kurt