Limit characters in HTML Field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2022 08:25 AM
Hi,
I'm trying to limit characters in HTML field. Any suggestions? I used the code from following website but onkeyup function doesn't work
(https://servicenowguru.com/scripting/client-scripts-scripting/maximum-length-large-string-fields-servicenowcom/https://servicenowguru.com/scripting/client-scripts-scripting/maximum-length-large-string-fields-servicenowcom/).
function onLoad() {
//Use the 'onkeyup' event to check the 'isMaxLength' function
//Get the control
var control = g_form.getControl('comments');
//Set its onkeyup method
control.onkeyup = isMaxLength;
}
function isMaxLength(){
var mLength=500;
var control = g_form.getControl('comments');
if (control.value.length > mLength){
g_form.hideErrorBox('comments');
g_form.showErrorBox('comments', 'You have reached the maximum character limit for this field.');
control.value=control.value.substring(0,mLength);
}
else{
g_form.hideErrorBox('comments');
}
}
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2022 08:30 AM
Hey,
You can limit the variable field on dictionary level itself.
That you can do using Configure table, and you can define what field size you want.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2022 05:19 PM
Hi AH,
One thing with HTML field the data is saved with HTML tags so the number of characters is going to be length of data entered + length of HTML tags. That is, setting more style will reduce the number of characters that may be entered.
As is written in the following Now Support page, setting max_length in a dictionary is not enforced.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0685779
I've answered this question before. I'll try to find the script I've used and reply if I can find it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2022 05:22 PM
Following script will check the length of html field when the form is submitted.
var maxLength = 20; // max length of html field
function onSubmit() {
var text = g_form.getValue('text');
var html = g_form.getValue('html');
var cleanText = domParser(html);
return false;
}
function domParser(str) {
var parser = new DOMParser();
var htmlDom = parser.parseFromString(str, "text/html");
var length = 0;
//alert(htmlDom.documentElement.innerHTML);
var len = traverseDom(htmlDom, 0);
if (len > maxLength) {
g_form.setValue('html', htmlDom.documentElement.innerHTML);
g_form.showFieldMsg('html', 'HTML field must be less than ' + maxLength + ' characters.', 'error');
}
return htmlDom.documentElement.innerHTML;
}
function traverseDom(xmlDoc, total) {
if (!xmlDoc.hasChildNodes) {
return;
}
var children = xmlDoc.childNodes;
for (var i = 0; i < children.length; i++) {
var curNode = children[i];
if (curNode.nodeType == 3) {
var strValue = curNode.nodeValue;
var subTotal = total + strValue.length;
if (subTotal >= maxLength) {
if ((maxLength - total) > 0) {
curNode.nodeValue = strValue.substring(0, (maxLength - total));
} else {
curNode.nodeValue = "";
}
}
if (strValue != null) {
total += strValue.length;
}
}
total = traverseDom(curNode, total);
}
return total;
}
Execution sample