Regex in onchange catalog client script

tsoct
Tera Guru

The catalog client script should retrieve the regex pattern from the country's table and compare it to the input in the 'account_format' variable.

However, the script below is not working. I confirmed that the regex pattern is correct, however it does not display a warning message when the input value is incorrectly formatted.

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 pattern = rec.u_pattern; //pattern = regex format stored as string

            if (!pattern.test(newValue)) {
                alert('Invalid input');
                g_form.clearValue('account_format');

            }
        }
    });

}

 

1 ACCEPTED SOLUTION

Did you check your browser console, the below will let you know whether a pattern is found

console.log("Found pattern of " + pattern);

View solution in original post

3 REPLIES 3

Kieran Anson
Kilo Patron

It's strongly advised to not use GlideRecord client side and instead use GlideAJAX. However to resolve your query:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ctry = g_form.getValue('bank_country');

    var countryGR = new GlideRecord('core_country');
    countryGR.setLimit(1);
    countryGR.query('sys_id', ctry, function(responseGR) {
        if (!responseGR.next())
            return alert("No Record Found");

		var pattern = responseGR.u_pattern;
		console.log("Found pattern of " + pattern);

		if(!pattern.test(g_form.getValue('field_name_here'))){
			alert("Invalid input");
		}
    })
}

Thanks but this is not returning any alert on wrong input

Did you check your browser console, the below will let you know whether a pattern is found

console.log("Found pattern of " + pattern);