Javascript validate number commas and decimal [closed]

I want to return the number true if it is a valid number that contains only digits with properly placed decimals and commas, otherwise return the number false. For example: if number is 1,093,222.04 or 0.232567 then my program should return the number true, but if the input were 1,093,22.04 then my program should return the number false. For example:

input: 1,093,222.04 => true
input: 0.232567 => true
input: 1267 => true
input: 1,093,22.04 => FALSE
input: 1.282,04 => FALSE
input: abcd124 => FALSE

I tried this but 1,093,222.04 returns false. It should return true

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

Answer

Try using regex instead

const isNumber = x => !!`${x}`.match(/^d*(,d{3})*(.d*)?$/)

console.log(isNumber("1,093,222.04")) // true
console.log(isNumber("0.232567")) // true
console.log(isNumber("1267")) // true
console.log(isNumber("1,093,22.04")) // false
console.log(isNumber("1.282,04")) // false
console.log(isNumber("abcd124")) // false

Do note that the function must take in a string, since if you pass in the number itself, the commas , will split the “number” into different parameters