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

Abhijit4
Mega Sage

Hi Sirraj,

You can try other way around, below script will written false for your first two scenario and it will return true whenever there will be any special character in string.

 

var specials = /[^A-Za-z0-9 ]/g;

gs.print(specials.test("Abel tuter")); // this prints false

 

var specials = /[^A-Za-z0-9 ]/g;

gs.print(specials.test("Abel")); // this prints false

 

var specials = /[^A-Za-z0-9 ]/g;

gs.print(specials.test("Abel%tuter")); // this prints true

 

You can use not (!) with result to get your required output.

e.g.

var specials = /[^A-Za-z0-9 ]/g;

gs.print(!specials.test("Abel")); // this prints true

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

Community Alums
Not applicable

Hi Abhijit,

Thank you for the response. The script which you gave didnt worked.

I have used this regExp and it is working fine. But validating spaces also, i dont want to validate space.

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

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

Community Alums
Not applicable

Thank you abhjit. Marked as corrrect.