- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2022 10:09 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:07 AM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2022 11:10 PM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 12:47 AM
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](,)?)*)+$/)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:07 AM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:44 AM
Thank you abhjit. Marked as corrrect.