- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2018 06:58 PM
Has anyone been able to build a dynamic regex string in a Catalog Client Script to validate an entry in a service catalog field. We want to check that the IBAN field is filled in with a country-specific two letter prefix and be a country specific length. I have stored the prefix and length in the Countries table and the validation will be trigger as an OnChange script. I tried the script below, but it does not seem to do the test correctly. I confirmed that the variable is being set to what I want the regex string to be. (You can see the correct string in the commented line.) It just doesn't seem to evaluate the .test with the variable. Ideas appreciated!
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ctry = g_form.getValue('bank_country');
var rec = new GlideRecord('core_country');
rec.addQuery('sys_id', ctry);
rec.query(function(){
if (rec.next()) {
var ilength = rec.u_iban_length;
var inumb = ilength - 2;
var iprefix = rec.u_iban_prefix;
var valid = "/^" + iprefix + "\[0-9A-Z]{" + inumb + "}$/";
alert(valid);
// if (/^AD[0-9A-Z]{22}$/.test(newValue))
if (valid.test(newValue))
{
return (true);
}
else
{
alert('Please enter a valid IBAN for ' + rec.name + ' starting with ' + iprefix + ' and containing a total of ' + ilength + ' characters.');
g_form.setValue('bank_iban','');
return (false);
}
}
});
}
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2018 06:15 AM
Yes, see https://stackoverflow.com/questions/17885855/use-dynamic-variable-string-as-regex-pattern-in-javascr... for an example, also pasted below;
var stringToGoIntoTheRegex = "abc";
var regex = new RegExp("#" + stringToGoIntoTheRegex + "#", "g");
// at this point, the line above is the same as: var regex = /#abc#/g;
var input = "Hello this is #abc# some #abc# stuff.";
var output = input.replace(regex, "!!");
alert(output); // Hello this is !! some !! stuff.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2018 06:15 AM
Yes, see https://stackoverflow.com/questions/17885855/use-dynamic-variable-string-as-regex-pattern-in-javascr... for an example, also pasted below;
var stringToGoIntoTheRegex = "abc";
var regex = new RegExp("#" + stringToGoIntoTheRegex + "#", "g");
// at this point, the line above is the same as: var regex = /#abc#/g;
var input = "Hello this is #abc# some #abc# stuff.";
var output = input.replace(regex, "!!");
alert(output); // Hello this is !! some !! stuff.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2018 12:04 PM
Worked like a charm. Thanks for the info and the reference!