Regular Expression to validate comma separated numbers using javascript

Today i got situation to validate text field containing comma separated numbers. I know how to validate numbers alone. But don’t know how to validate numbers with comma separated and each number should be 8 digits.

For example, 1234678,54872659,84567892,....

I searched google but didn’t find it and finally i got solution from my friend…

Here is the regular expression to do this trick,

/^(?:\s*\d{8}\s*(?:,|$))+$/

Usage:

<script language="javascript">
var pattern = /^(?:\s*\d{8}\s*(?:,|$))+$/;
var testvalue = document.getElementById('checkNumber').value;
if(pattern.test(testvalue)){
alert("Correct");
} else {
alert("Wrong");
}
</script>

 

More explanations on this regular expression, please check here

http://regex101.com/r/iY9bQ9

Hope this will be useful to you all.

Author: Vinodkumar Saravana.