Skip to content

Commit be10dff

Browse files
README.md
1 parent 5c5434f commit be10dff

File tree

1 file changed

+71
-0
lines changed
  • Client-Side Components/Client Scripts/Field Character Counter

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Live Character Counter Client Script
2+
This ServiceNow client script provides a live character counter for a specified text field, offering immediate feedback to the user as they type. The script visually alerts the user when they are nearing or have exceeded the maximum character limit.
3+
Features
4+
Real-time feedback: The script updates the character count as the user types.
5+
Visual cues: Different message styles are used to indicate the character count status:
6+
Info: Standard message showing the remaining characters.
7+
Warning: Displays when 10 or fewer characters remain.
8+
Error: Appears when the maximum character limit is exceeded.
9+
Clean user experience: The script clears previous messages before displaying a new one, preventing duplicate messages from cluttering the form.
10+
Best practices compliant: The script uses g_form methods exclusively and avoids direct DOM manipulation, ensuring compatibility and maintainability across ServiceNow upgrades.
11+
Installation
12+
1. Create the Client Script
13+
Navigate to System Definition > Client Scripts.
14+
Click New.
15+
Fill out the form with the following details:
16+
Name: onChange - Live Character Counter
17+
Table: Select the table where the target text field exists (e.g., Incident).
18+
UI Type: All
19+
Type: onChange
20+
Field name: The name of the text field you want to monitor (e.g., short_description).
21+
Script: Copy and paste the code snippet below.
22+
2. Configure the script
23+
Paste the following code into the Script field. Be sure to change the fieldName variable to match the internal name of your target field.
24+
javascript
25+
function onChange(control, oldValue, newValue, isLoading) {
26+
if (isLoading || newValue === '') {
27+
return;
28+
}
29+
30+
// Set the internal name of the field to monitor.
31+
var fieldName = g_form.getControl('name').name; // This dynamically gets the field name.
32+
33+
// Fallback in case the control isn't found.
34+
if (!fieldName) {
35+
fieldName = control.name;
36+
}
37+
38+
var maxLength = g_form.getControl(fieldName).getAttribute('maxlength');
39+
if (maxLength) {
40+
41+
var remaining = maxLength - newValue.length;
42+
var message = 'Remaining characters: ' + remaining;
43+
44+
// Clear previous messages to avoid duplication.
45+
g_form.hideFieldMsg(fieldName);
46+
47+
if (remaining <= 10 && remaining >= 0) {
48+
g_form.showFieldMsg(fieldName, message, 'warning');
49+
} else if (remaining < 0) {
50+
g_form.showFieldMsg(fieldName, 'Maximum characters exceeded!', 'error');
51+
} else {
52+
g_form.showFieldMsg(fieldName, message, 'info');
53+
}
54+
}
55+
}
56+
Use code with caution.
57+
58+
How it works
59+
Event trigger: The script is triggered every time the user modifies the selected onChange field.
60+
Initial check: The script checks for isLoading or an empty newValue to prevent unnecessary execution on form load or when the field is empty.
61+
Get field details: It retrieves the maxlength attribute from the field's control object.
62+
Calculate remaining characters: It subtracts the current input length from the maximum length.
63+
Display message: It uses conditional logic to display a message with a specific style (info, warning, or error) based on the remaining character count.
64+
Clear messages: g_form.hideFieldMsg() ensures only the most current message is visible at any time.
65+
Customization
66+
Target Field: Change the Field name in the Client Script configuration to apply the counter to a different field.
67+
Thresholds: Modify the remaining <= 10 condition to change the number of characters that trigger the warning message.
68+
Message Text: Adjust the message variables to customize the text displayed to the user.
69+
70+
71+

0 commit comments

Comments
 (0)