Restriction of characters on short description field on Record Producer in Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2023 04:49 AM
Hello Team,
I have a requirement to restrict the length of the short description field in the record producer on the Portal, if users try to do it it cannot allow/ popup the alert. I have used the on-change catalog client script it is working fine only when we move the cursor to another field.
if(newValue.length() > 20)
{
alert("message");
}
I have also used onload client script, it is working fine when i am trying from native ui using "try" UI Action.
function onLoad() {
//Use the 'onkeyup' event to check the 'isMaxLength' function
//Get the control
var control = g_form.getControl('short_description');
//Set its onkeyup method
control.onkeyup = isMaxLength;
}
function isMaxLength() {
var mLength = 20;
var control = g_form.getControl('short_description');
if (control.value.length > mLength) {
g_form.hideErrorBox('short_description');
g_form.showErrorBox('short_description', 'You have reached the maximum character limit for this field.');
control.value=control.value.substring(0,mLength);
} else {
g_form.hideErrorBox('short_description');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2023 04:21 PM
It sounds like you have a working solution for the onload client script, but it's not working for the record producer on the Portal when users try to exceed the character limit.
One approach you could try is to modify the client script to also include an onkeyup event listener for the short_description field. This way, the script will run as users type into the field and prevent them from exceeding the character limit without needing to move to another field.
Here's an example of what the modified client script might look like:
function onLoad() {
// Set up event listener for onkeyup
var shortDescription = g_form.getControl('short_description');
shortDescription.addEventListener('keyup', function() {
var maxLength = 20;
var currentValue = shortDescription.value;
if (currentValue.length > maxLength) {
alert('Maximum character limit reached!');
shortDescription.value = currentValue.substring(0, maxLength);
}
});
}
This script uses addEventListener to set up a listener for the keyup event on the short_description field. When the event is triggered, it checks the length of the current value in the field and compares it to the maximum length. If the current value exceeds the maximum length, it displays an alert message and truncates the value to the maximum length.
You can modify the alert message and the maximum length to fit your specific requirements.
I hope this helps! Let me know if you have any further questions or need more information.
Please mark my answer as a solution/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2023 11:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2023 11:07 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2023 08:30 AM
Hi @Ankit Pratap009 ,
There is one OOB variable attribute "max_length" that can use to restrict the character limit.
For example, to allow for entry of an Short description, set max_length=20, or other appropriate length as per requirement.
Applicable variables: Single-line text, Wide single-line text.
Please mark the reply as Helpful/Correct/Accept Solution, if applicable. Thanks!
Thanks & Regards
Jyoti Jadhav