Set Character limit on Multi-line Variables for 4 variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Set Character limit on Multi-line Variables for 4 variables : We have 4 different variables in a catalog form where we have to set limit for them for eg : Description(400), Name(250), Stock(350), Lines(380) how to achieve this pls let me know with less code and as per the best practise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
i didnt get a user should get a error message if he cross the limit while filling out the catalog form. Suppose he is entering description then he should get message if crosses 400 characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @LearnerSN ,
If you want to display a character limit message whenever a variable's value changes, you need to create four separate onChange Catalog Client Scripts, one for each variable.
However, if you want to handle the character limit validation for all four variables in a single script, you can use an onSubmit Catalog Client Script. This script will execute when the user attempts to submit the catalog item and display the appropriate character limit error message if any variable exceeds its allowed limit.
This approach allows you to handle all four variable validations in a single script, rather than creating four separate client scripts.
function onSubmit() {
var maxLength = 5;
var fieldsToValidate = [
'test',
'test1',
'test2'
];
var isValid = true;
// 3. Loop through each variable to check the length
for (var i = 0; i < fieldsToValidate.length; i++) {
var fieldName = fieldsToValidate[i];
var fieldValue = g_form.getValue(fieldName);
g_form.hideFieldMsg(fieldName, true);
if (fieldValue && fieldValue.length > maxLength) {
g_form.showFieldMsg(
fieldName,
'Maximum limit of ' + maxLength + ' characters exceeded. You have used ' + fieldValue.length + ' characters.',
'error'
);
isValid = false;
}
}
// 4. Block submission if any of the 4 fields failed validation
if (!isValid) {
return false;
}
}
If this helps you then mark it as helpful and accept as solution .
Regards,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
There is an alternative to it if you dont want that much client scripts.
Go to All > Variable Validation Regex
Create a new record
400 char limit
Add this in regex: ^[\s\S]{0,400}$
Add Validation message: Input is exceeding 400 characters
Save the record.
In the variable record, go to Type Specifications section > Validation Regex field, add this Validation Regex you created.
No script needed, it will validate 400 character limit.
If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.
Kind Regards,
Ehab Pilloor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
i need to create this for all 4 variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Yes but you will not need 4 client scripts, also those regex may be useful in other variables, later on.
If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.
Kind Regards,
Ehab Pilloor