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

I am not an expert in coding, but something

If Country = xx

Then 

Value some things.

*************************************************************************************************************
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]

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

Harrymate
Kilo Guru

You can enforce this character limit restriction dynamically using Client Scripts:

 

using Client Scripts >>  Go to the Catalog Client Scripts >> Click New 

 

in there Type should be onChange and pass the Variable Name country (Make sure this matches your Country field variable name)

 

 // Check if Country is Thailand
    if (newValue === "Thailand") {
        // Define Address Field values
        var addressValues = ["address_line_1", "address_line_2", "address_line_3"];

        addressValues.forEach(function(field) {
            var addressField = g_form.getControl(field);
            if (addressField) {
                // Set Maximum Length to 70
                addressField.maxLength = 70;
                g_form.showFieldMsg(field, "Maximum 70 characters allowed for Thailand.", "info");
            }
        });
    } else {
        // Remove field restriction if not Thailand
        var addressValues = ["address_line_1", "address_line_2", "address_line_3"];
        addressValues.forEach(function(field) {
            var addressField = g_form.getControl(field);
            if (addressField) {
                addressField.removeAttribute("maxLength");
                g_form.clearMessages();
            }
        });
    }

Thanks a lot Harry. 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
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;
   
}