How to validate comma separated numbers using javascript

Created at 07-Sep-2020 , By samar

In this article we will learn how to validate comma separated value in javascript. We are not just provide you a code but we will also tell you the code work flow , application of this codes and where to use it.

We are going to use the regex method to validate comma separated value in this article. This function will return true or false as per your input. If the assigned value is valid then it will return true  and if the assigned value is invalid then it will return the false. So using this method you can check the value we passed to this regex is a valid comma separated value or not.

Application of below code.

1. To insert value in table cell of excel file. Before insert value we have to check the value passed to function is a valid value or not. 

2. To check the passed value to regex  is a numeric value or a non numeric value.


<script>

let regex = /[0-9]+(,[0-9]+)*/g
console.log('1231232,12323123,122323',regex.test('1231232,12323123,122323')); 
console.log('1,22,34,25,645,',regex.test('1,22,34,25,645,'));
console.log('1',regex.test('1')); 

</script>

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.