- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 07:20 AM
If a service catalog variable has "Validation Regex" on it, has anyone figured out how to tell if the field is not passing validation if it has a corresponding "onChange" client script?
Of course, I can just repeat the previously used Regex within my "onChange" client script, but ideally, I thought there would be a property of the field that I could check, so I would know not to continue with my "onChange" client script if the new field value itself isn't even passing the "Validation Regex" set in place.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 10:40 AM
Hello @MG Casey
Unfortunately there isn't a easy way to do this. Some work around is possible like
- Solution 1: As you mentioned Repeat the Regex in the Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Regex pattern (same as the one in Validation Regex)
var regex = /^[A-Za-z0-9]+$/; // example pattern, update accordingly
if (!regex.test(newValue)) {
g_form.showFieldMsg('variable_name', 'Invalid input. Please correct the format.', 'error');
return;
}
// Continue with the rest of the onChange logic if input is valid
// Your additional logic here
}
Solution 2: Use isValidRecord() (Custom Solution)
Another approach is to write a Script Include that checks the validity of the field against the server-side Validation Regex. You can then call this script from your client script using a GlideAjax call. However this is not recommended.
"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 10:40 AM
Hello @MG Casey
Unfortunately there isn't a easy way to do this. Some work around is possible like
- Solution 1: As you mentioned Repeat the Regex in the Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Regex pattern (same as the one in Validation Regex)
var regex = /^[A-Za-z0-9]+$/; // example pattern, update accordingly
if (!regex.test(newValue)) {
g_form.showFieldMsg('variable_name', 'Invalid input. Please correct the format.', 'error');
return;
}
// Continue with the rest of the onChange logic if input is valid
// Your additional logic here
}
Solution 2: Use isValidRecord() (Custom Solution)
Another approach is to write a Script Include that checks the validity of the field against the server-side Validation Regex. You can then call this script from your client script using a GlideAjax call. However this is not recommended.
"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 12:34 PM
Ya, that's too bad, so solution 1 is what I had already ended up doing.