-2

How do I additionally test for numbers like 01 or 0080?

This is my current test.

const arr = ["01/05/2023", "1ert", "0fer", "blah", "0", "", "123", "1e3", "1*4", "0080", "1.34", "1,00", "-2"]

function validNumber (a) {
  let regex = /^\d+$/;
  a.forEach((string) => {
    if(!string.match(regex)) {
        console.log("not a number:", string)
    } else {
      console.log(parseInt(string))
    }
  })
}

this test works however I dont want users thinking they can input numbers with leading zeros.

JayyRon
  • 29
  • 4
  • Try `/^[1-9]\d+$/` but [there might be a better way](https://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer). – Corey Ogburn Sep 01 '22 at 23:01
  • @CoreyOgburn: I think it should be: `/^[1-9]\d*$/` - otherwise you require 2 digits. – Poul Bak Sep 01 '22 at 23:26

1 Answers1

0
function validNumber (a) {
  let regex = /^\d+$/;
  a.forEach((string) => {
    if(!string.match(regex)) {
        console.log("not a number:", string)
    } else {
      console.log(parseInt(string));
      if (string.match(/^0+\d+$/)) {
        console.log("numbers starts with zeros -- don't do that dear user!");
      }
    }
  })
}
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71