-1

I need to create a function that will get parameters as string and check if that string param contains only numbers or not. I tried doing this but I m not sure of the REGEX value.

function validateNum(x) {
  var regex = /^\d+$/

  var isValid = regex.test(x.value);
  if (isValid) {
    console.log("This is a Number. Thus, Accepted!.");
  } else {
    console.log("Only Numbers allowed.");
  }
  return isValid;
}
console.log(validateNum("12x3aj")) //Only Numbers allowed.
console.log(validateNum("12345")) //This is a Number. Thus, Accepted!.

Can anyone help me in this?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
4N335
  • 267
  • 2
  • 29

1 Answers1

-1

Seems you wanna something like Array.from(myString).every(Number.isInteger).

Zazaeil
  • 3,900
  • 2
  • 14
  • 31