JSFiddle - React, Tailwind, and code Playground

by TheDiamondDoge

JavaScript

function test(val, optionalRegexTest) {
  const value = Number.isNaN(val) ? 0 : val;

  if (optionalRegexTest && optionalRegexTest.test(String(value))) {
    return true;
  } else {
    return false;
  }
}

console.log(test(1, /^\d+%?$/));
console.log(test(10, /^\d+%?$/));
console.log(test("1%", /^\d+%?$/));
console.log(test("%1%", /^\d+%?$/));
console.log(test("1.1", /^\d+%?$/));