Catalog Item numeric variable constraint

dev_K
Tera Contributor

Hello,

 

I have a catalog item where one field is a text variable that uses regex to ensure only numeric value is entered by users. How can I add restrictions to it? I want user to be able to enter value that is not bigger than 100.

 

Thanks!

3 REPLIES 3

Akshay03
Kilo Sage

Hello @dev_K ,

Please use onchange client script of it 

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


var regexPattern = /^[0-9]+$/;

    if (regexPattern.test(newValue) && parseInt(newValue, 10) <= 100) {
        alert('ok');
    } else {
        alert('add number less than 100');
		g_form.clearValue('number');
    }
}


Regards,
Akshay

SN_Learn
Kilo Patron
Kilo Patron

Hi @dev_K ,

 

You can use the OOB variable regex named Number as below:

SN_Learn_0-1718782471848.png

 

In the variable attribute,

max_length=100


This will ensure not more than 100 is entered.

 

Please Mark My Response as Correct/Helpful based on Impact

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Hi @dev_K,

 

Please find below script for the same. 

 

@function onChangeNumericField()

    var fieldValue = g_form.getValue('your_text_variable'); // Replace 'your_text_variable' with the actual field name

 

    // Check if the field value is numeric and within the desired range

    if (fieldValue !== '' && !isNaN(fieldValue)) {

        var numericValue = parseFloat(fieldValue);

        if (numericValue > 100) {

            // Clear the field value or display an error message

            g_form.setValue('your_text_variable', ''); // Clear the field value

            alert('Please enter a value equal to or less than 100.');

        } else {

            // Value is valid, proceed with any additional logic

            // For example, you may want to perform further actions or validations here

        }

    } else {

        // Handle non-numeric input

        g_form.setValue('your_text_variable', ''); // Clear the field value

        alert('Please enter a numeric value.');

    }

}

 

Please accept my solution if it resolves your query and thumps 👍 up. 

 

Thanks 

Jitendra 

Please accept my solution if it works for and thumps up.