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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @AbdurRahmanSnow 

Add this attribute

 

AGLearnNGrow_0-1740575733473.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Yes Sir, but it should only apply to Thailand country and not to other countries. How can we do it?

Then you need to use the onsubmit script and add this field count.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

since you said combination of all 3 should be max 70,

you can use onSubmit, get values from all 3 variables and check max length
OR

You can use single line text variable and give max_length=70 in variable attributes

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