How to restrict max length of characters, on a Catalog Form field, based on a dependent field ?

AbdurRahmanSnow
Giga Guru

Good evening
On a Catalog form, based on Country is Thailand, then it should allow the below 3 Address line Fields to only take 70 characters/digit length.
How can I do this? Please guide.

AbdurRahmanSnow_1-1740575420557.png

AbdurRahmanSnow_2-1740575445820.png

 

1 ACCEPTED SOLUTION

AbdurRahmanSnow
Giga Guru
Correct Answer

function
onSubmit() {
   //Type appropriate comment here, and begin script below

   var country = g_form.getDisplayValue('country');

   if (country == 'THAILAND') {
      var add1 = g_form.getValue('Address1');
      var add2 = g_form.getValue('Address2');
      var add3 = g_form.getValue('Address3');

      //alert('Address lengths: ' + add1.length + ', ' + add2.length + ', ' + add3.length);

      if ((add1.length > 70) || (add2.length > 70) || (add3.length > 70)) {
        alert('The maximum character limit for addresses in Thailand is 70.!');
        return false;
      }
   }  
   return true;
   
}

View solution in original post

13 REPLIES 13

Oh I see. Interesting Ankur. Can you please give me the code? I am confused, how can I check the variables length...

@AbdurRahmanSnow 

something like this in onSubmit

function onSubmit() {
    // Get the country value
    var country = g_form.getValue('countryVariable');

    // Check if the country is Thailand
    if (country == 'ThailandSysId') {
        var val1 = parseInt(g_form.getValue('variable1'));
        var val2 = parseInt(g_form.getValue('variable2'));
        var val3 = parseInt(g_form.getValue('variable3'));
        if (val1 + val2 + val3 > 70) {
            g_form.addErrorMessage('Max address is 70 for Thailand');
            return false;
        }
    }
}

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

@AbdurRahmanSnow 

Thank you for marking my response as helpful.

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

Thanks a lot Ankur. I just modified and got the code as below:

Correct Answer

function onSubmit() {
   //Type appropriate comment here, and begin script below

   var country = g_form.getDisplayValue('country');

   if (country == 'THAILAND') {
      var add1 = g_form.getValue('Address1');
      var add2 = g_form.getValue('Address2');
      var add3 = g_form.getValue('Address3');

      //alert('Address lengths: ' + add1.length + ', ' + add2.length + ', ' + add3.length);

      if ((add1.length > 70) || (add2.length > 70) || (add3.length > 70)) {
        alert('The maximum character limit for addresses in Thailand is 70.!');
        return false;
      }
   }  
   return true;
   
}

@AbdurRahmanSnow 

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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