Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To validate comma separated numbers string using regex, use /^[0-9]+(,[0-9]+)*$/ it will handle comma separated numbers.

Let’s see short example of javascript regex for comma separated numbers.

const regex = /^[0-9]+(,[0-9]+)*$/;
console.log(regex.test("1,284,473"))

Today, I’m going to show you How do I check value contain comma separated numbers in javascript, as above mentioned, I’m going to use the above-mentioned regex with test() method.

Let’s start today’s tutorial how do you check comma separated numbers in javascript using regex?

Here, I will show validate comma separated numbers in javascript and typescript.

Javascript regex for comma separated numbers example

Here, we will create common validate function where we will validate param should comma separated numbers.

function validate(param){
  const regex = /^[0-9]+(,[0-9]+)*$/;
  return regex.test(param);
}

// validate empty string
console.log(validate(""))
// false

// validate decimal number
console.log(validate("435.58"))
// true

// validate comma separated numbers
console.log(validate("1,464,346"))
// true

// validate wrong comma separated numbers
console.log(validate("1,464,346,"))
// false

Output

Typescript regex for comma separated numbers example

Same like javascript, we will create a function and call it with diffrent parameters.

function validate(param: string){
  const regex = /^[0-9]+(,[0-9]+)*$/;
  return regex.test(param);
}

// validate empty string
console.log(validate(""))
// false

// validate decimal number
console.log(validate("435.58"))
// true

// validate comma separated numbers
console.log(validate("1,464,346"))
// true

// validate wrong comma separated numbers
console.log(validate("1,464,346,"))
// false

Output

I hope it helps you, All the best đź‘Ť.