Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to not include letters and values without spaces after commas

I have a text box and I need to validate the date that is being input. I’d like the users to only input numbers and if they input multiple numbers separated by a comma I need them to always add a space after the comma. I want to only accept data that looks like this: 1234, 15482, 086392 or 16843
I don’t want to accept data if it looks anything like this: 13738,74848, abc , 6a738

I’m using a google form here so I need a Regex that matches the above criteria.

I have two Regex ,[^ ] and [A-Za-z] but I can’t figure out how to connect them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could write the pattern as:

^\d+(?:, \d+)*$

Explanation

  • ^ Start of string
  • \d+ Match 1+ digits
  • (?:, \d+)* Optionally repeat matching a comma and a space followed by 1+ digits
  • $ End of string

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading