0

I need to limit input to only one number and one decimal. For example to be able to put 9.5 but not 95. Only thing I could think of is something like this:

    let length = e.currentTarget.textContent.length;

    if (length >= 3) {
      console.log(length);
      e.preventDefault();
    }
Aleksa
  • 1
  • 2
  • Refer this [Limit decimal places in number](https://stackoverflow.com/questions/39521122/javascript-regex-for-decimal) – Gaurav Agam Jul 24 '20 at 12:52

1 Answers1

0

Use this function

function numberValidator(input) {
    if (input.length < 4)
        if (typeof input == 'string') {
            if (input.indexOf('.') == 0 || input.indexOf('.') == 2)
               return false;
            input = parseFloat(input).toFixed(1)
            if (parseFloat(input) < 10) {
                console.log(input);
                return true
            }
        }
   return false
}
if (numberValidator(e.currentTarget.textContent)) {
    // setState
} else {
    // e.preventDefault(); or dont set state
}

Let me know if it dosen't work,Upvote if works:-)

Hrishi
  • 1,210
  • 1
  • 13
  • 25