-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?

1
  • 1
    Strings don't have a value property. Just use regex.test(x) Commented Mar 1, 2020 at 7:14

1 Answer 1

-1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.