Show Character Counter for Form Fields in ServiceNow Without Risky DOM Manipulation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2025 10:31 PM
SUMMARY:
When entering names or descriptions in fields like Client Scripts, Business Rules, or Scheduled Jobs, users often hit the maximum character limit unexpectedly. This results in rewriting and a poor user experience.
This article provides a simple and safe solution that shows remaining characters for specific fields, improving form usability, without using unsafe DOM manipulation techniques.
Use Case:
You're creating or editing records in tables like:
sys_script (Business Rules)
sys_client_script (Client Scripts)
sys_trigger (Scheduled Jobs)
You try typing in the Name or Short description fields, and suddenly you hit a max character limit without any warning. This script shows the remaining characters live, just below the field, to avoid surprises.
surprises.
Key Features:
Works on standard forms (not just Service Portal)
No risky HTML DOM manipulation
Displays a live character counter
Optionally shows a warning message when the limit is exceeded
Easy to extend to multiple fields and tables
Script (Client Script – Type: onLoad, UI Type: All, Isolate Script: Unchecked):
Why Isolate Script = Unchecked?
To access DOM-like properties such as g_form.getControl(fieldName).getAttribute('maxlength'), we must disable script isolation.
This is safe here because:
We’re only reading attributes.
We’re not modifying any layout or DOM nodes dangerously.
All changes are limited to UX enhancement, not structure.
How to Apply:
Navigate to System Definition > Client Scripts
Create a new onLoad Client Script
Target any table (like sys_script or sys_client_script)
Paste the code above
Uncheck "Isolate Script."
Test it on your form – you’ll now see a live character counter below the field(s)!
Reusable & Extendable:
You can use the same logic for:
Any custom or out-of-box field with a character limit
Other tables are created by reusing the same script
Portal widgets (with slight changes)
Note:
Avoid using document.getElementById() or modifying HTML content directly. This solution stays within ServiceNow's supported boundaries while achieving a better user experience.
Result:
Your users will now see how many characters they have left as they type—making forms smarter, cleaner, and frustration-free!
📸 Screenshots / Output Preview:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2025 10:41 PM
It does involve DOM manipulation which is not recommended.
Why would admin require this functionality? Any use-case you have which you faced recently working on any story?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader