Service Catalog Variable that has "Validation Regex" in Place vs. onChange Script

MG Casey
Mega Sage

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.

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

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

 

View solution in original post

2 REPLIES 2

Juhi Poddar
Kilo Patron

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

 

Ya, that's too bad, so solution 1 is what I had already ended up doing.