Regex expression to check comma separated string

Community Alums
Not applicable

Hi All,

I need to validate a single line text field with only comma separated values.

Case 1 : With one input it should return true, example: Abel

Case 2 : With multiple input it should return true, example: Abel tuter, Sys Admin

Case 3: Any special characters except commas it should return false, example: Abel tuter,/Sys Admin (or) Abel tuter,%Sys Admin.

Do we have any regex for that. I have tried one or two it didn't worked.

Regards,

Sirraj

1 ACCEPTED SOLUTION

Hi,

My code should work, I have tested in my PDI.

Anyway, you can add space in between '9' and  ']' that will make it validate space as well. As below, I have made that part bold there is space.

var regex = (/^(([a-zA-Z0-9 ](,)?)*)+$/)

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

6 REPLIES 6

Willem
Giga Sage
Giga Sage

Hi Sirraj,

Try like this:

var regex = (/^(([a-zA-Z0-9\s](,)?)*)+$/);
var inputStr = "Abel ";
gs.print(regex.test(inputStr));

 In addition, have a look at an Article I have written about Regular expression, that might give you some ideas as well:

https://community.servicenow.com/community?id=community_article&sys_id=76f31748db11b4d06064eeb5ca961...

Community Alums
Not applicable

Thank you Willem, its a nice article. One last query i need to allow white spaces between commas. But not after commas.

For ex - "Abel Tuter" should return true.

But "Abel Tuter, Sys Admin" should return false. After comma i dont need space. I saw your article, i guess i should do something with /S. But where exactly i need to paste this in my reg ex.