-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
  • 313,535
  • 40
  • 245
  • 254
curiousKido
  • 242
  • 3
  • 15

1 Answers1

-1

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

Zazeil
  • 3,465
  • 2
  • 11
  • 28