Regex for validation numbers with commas and spaces

I'm not sure why this regex expression is not working. I want to check if there is an input in this format:

  1. 12345678,12345678,12345678
  2. * space * 12345678, 12345678, 12345678, 12345678
  3. 12345678,12345678, space
It must be 8 digits if it does not return false. Below is expressed the regex expression that I made, but it works for 2 sets of numbers, but when I entered another set of numbers, validation does not work.
  1. Work: 12345678, 12345678
  2. Does not work: 12345678, 12345678, 12345678
var validate_numbers = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?$/; 

thanks

+4
source share
3 answers

You need to describe what you want to map in more detail. I assume that you want to match 8-digit numbers, separated by commas and pluses, possibly commas.

The problem is that you take no more than two sets of numbers. Visualization .

Given the assumption above, this is the regular expression you want:

 ^(\s*\d{8}\s*[+,]?\s*)*$ 

Again, you can visualize it on debuggex .

+2
source

Could you tell us a little more about the requirement? Do you need to have a space before the comma?

 \\d{8}(?:,\\d{8})*+ 

Try it out. it works great with a requirement that checks a list of numbers that have 8 digits and are separated by a comma.

Hope this helps

0
source

Remove '$' from the current regular expression. It strictly matches the end of the line, which leads to the fact that your expression returns false in your desired lines. The following code returns TRUE for the referenced rows that were previously returned FALSE.

omgerd I automatically wrote the first answer in PHP, here is a quick JS editing

 var pattern = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?/; var data2 = '12345678 , 12345678 ,12345678'; if (pattern.test(data2) != 0) { alert("ok"); } 

Conclusion: OK

0
source

All Articles