Field message not showing on catalog variable

DevtoSME
Giga Guru

I created a onchange client script to prevent special characters from being entered on the field but my field message is not displaying. can anyone help. is this not applicable or is there n issue with the code. i

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return; 
    }

    // Disallow special characters in abbreviation name
    var alpha = /^[\s0-9a-zA-Z]*$/;

    // 
    var isValid = alpha.test(newValue);

    if (!isValid) {
        // Show an error message
        g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');

        // Clear the invalid input 
        g_form.setValue('field_name', oldValue);
    } else {
        // Remove any previous error message if the input is valid
        g_form.hideFieldMsg('field_name');
    }
}

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Try a more traditional regex approach

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return; 
    }

    // Disallow special characters in abbreviation name
    var alpha = new RegExp("^[\s0-9a-zA-Z]*$");

    // 
    var isValid = alpha.test(newValue);

    if (!isValid) {
        // Show an error message
        g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');

        // Clear the invalid input 
        g_form.setValue('field_name', oldValue);
    } else {
        // Remove any previous error message if the input is valid
        g_form.hideFieldMsg('field_name');
    }
}

And if you are doing this on a Catalog Item variable as opposed to a form/table field, try the variable validation feature instead. 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Try a more traditional regex approach

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return; 
    }

    // Disallow special characters in abbreviation name
    var alpha = new RegExp("^[\s0-9a-zA-Z]*$");

    // 
    var isValid = alpha.test(newValue);

    if (!isValid) {
        // Show an error message
        g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');

        // Clear the invalid input 
        g_form.setValue('field_name', oldValue);
    } else {
        // Remove any previous error message if the input is valid
        g_form.hideFieldMsg('field_name');
    }
}

And if you are doing this on a Catalog Item variable as opposed to a form/table field, try the variable validation feature instead. 

i looked up the variable validation feature and did it that way. I wasn't privy to this addition. Thankyou 

SunnySharma
Tera Contributor

Hi DevtoSME,

 

In above code you need to just 'set the value' before the Field Error Message is shown.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return; 
    }

    // Disallow special characters in abbreviation name
    var alpha = /^[\s0-9a-zA-Z]*$/;

    // 
    var isValid = alpha.test(newValue);

    if (!isValid) {
        // Clear the invalid input 
        g_form.setValue('field_name', oldValue);

        // Show an error message
        g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');

    } else {
        // Remove any previous error message if the input is valid
        g_form.hideFieldMsg('field_name');
    }
}