Catalog Item numeric variable constraint
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 11:47 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2024 12:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2024 12:42 AM
Hi @dev_K ,
You can use the OOB variable regex named Number as below:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2024 12:49 AM
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