Catalog form validation

Prakash_S
Tera Contributor

I have one catalog item with filed Hardware Type (dropdown: Laptop, Desktop, Monitor, etc.) and justification.

My requirement is If "Hardware Type" is "Laptop" and the "Justification" field is less than 50 characters, display an error message.

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron

@Prakash_S 

you can use onChange catalog client script on Justification variable

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
g_form.hideFieldMsg('justification');
    // Check if the Hardware Type is 'Laptop'
    if (g_form.getValue('hardware_type') == 'Laptop') {        
        // Check if the Justification field has fewer than 50 characters
        if (newValue.length < 50) {
            g_form.showFieldMsg('justification', 'Justification must be at least 50 characters long.', 'error');
        } 
    } 
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Runjay Patel
Giga Sage

Hi @Prakash_S ,

You can write onChange clint script.

if (g_form.getValue('hardware_type') === 'Laptop') {
        g_form.setMandatory('justification', true);
        if (g_form.getValue('justification').length < 50) {
            g_form.showErrorBox('justification', 'Justification must be at least 50 characters for Laptop requests.');
        } else {
            g_form.hideErrorBox('justification');
        }
    } else {
        g_form.setMandatory('justification', false);
        g_form.hideErrorBox('justification');
    }

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

Here in this Video, I have covered the Custom Application Pattern Troubleshooting and configuration Thank you for visiting my channel. Here, I'll share various technical knowledge. Feel free to reach out to me directly for any Service Now-related queries. Your support encourages me to consistently
3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Prakash_S 

you can use onChange catalog client script on Justification variable

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
g_form.hideFieldMsg('justification');
    // Check if the Hardware Type is 'Laptop'
    if (g_form.getValue('hardware_type') == 'Laptop') {        
        // Check if the Justification field has fewer than 50 characters
        if (newValue.length < 50) {
            g_form.showFieldMsg('justification', 'Justification must be at least 50 characters long.', 'error');
        } 
    } 
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Prakash_S 

you will require 1 more onChange client script on Hardware type

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
g_form.hideFieldMsg('justification');
    // Check if the Hardware Type is 'Laptop'
    if (newValue == 'Laptop') {        
        // Check if the Justification field has fewer than 50 characters
        if (g_form.getValue('justification').length < 50) {
            g_form.showFieldMsg('justification', 'Justification must be at least 50 characters long.', 'error');
        } 
    } 
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Runjay Patel
Giga Sage

Hi @Prakash_S ,

You can write onChange clint script.

if (g_form.getValue('hardware_type') === 'Laptop') {
        g_form.setMandatory('justification', true);
        if (g_form.getValue('justification').length < 50) {
            g_form.showErrorBox('justification', 'Justification must be at least 50 characters for Laptop requests.');
        } else {
            g_form.hideErrorBox('justification');
        }
    } else {
        g_form.setMandatory('justification', false);
        g_form.hideErrorBox('justification');
    }

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Here in this Video, I have covered the Custom Application Pattern Troubleshooting and configuration Thank you for visiting my channel. Here, I'll share various technical knowledge. Feel free to reach out to me directly for any Service Now-related queries. Your support encourages me to consistently