0

I have a scenario where the HTML input field must take only 16 digit where 6 of its digits will be allocated for decimal places, but one strange thing happend in the code below is, when I add "0" at the end of decimal values, the digits are not being restricted and it keeps increasing. Is there anything that I'm missing out here?

<input type="number" name="val" min=0 max=9999999999.999999 step=".000001" save="" oninput="validity.valid ? this.save = value : value = this.save;"
Thiagz
  • 121
  • 1
  • 13
  • 1
    Well, 1,010 isn't bigger than 1,01. You have to check the length of your input with js – Benjamin Mar 12 '19 at 11:16
  • Acceptable but is there any other way than to use javascript @Cornflake – Thiagz Mar 12 '19 at 11:29
  • @Thiagz Can't you use the `maxlength="17"` attribute? (17 instead of 16 because the dot is also counted). – Ivar Mar 12 '19 at 12:22
  • 1
    You cannot, that's a limitation of type="number", same thing with maxlength ( discussed here https://stackoverflow.com/questions/18510845/maxlength-ignored-for-input-type-number-in-chrome, you'll find a quick script as the 2nd answer tho ). – Benjamin Mar 12 '19 at 12:30
  • What is this `[save]` attribute? – zer00ne Mar 12 '19 at 12:46
  • Its just a temporary variable to hold value. @zer00ne – Thiagz Mar 13 '19 at 02:55
  • If that's the case then `data-save="0"` `var v = document.querySelector('[name=val]').data('save')` – zer00ne Mar 13 '19 at 03:29

1 Answers1

0

Resolved the issue with the following code

<input type="number" name="val" min=0 max=9999999999.999999 step=".000001" save="" oninput="validity.valid && ((value.toString()).split('.')[1] === undefined || ((value.toString()).split('.')[1].length < 6)) ? this.save = value : value = this.save"/>
Thiagz
  • 121
  • 1
  • 13