- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 01:24 AM - edited 03-12-2025 01:24 AM
Hi
I am working on Onchange client script.
there is a field called description.
description should not allow special character but should allow hyphen.
it should only allow length = 32 char.
below is the script i have written.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regExp = /^[a-zA-Z0-9]+$/;
if (!regExp.test(newValue)) {
alert("Please Enter alpha-numeric value only");
g_form.clearValue('description');
}
}
how to restrict length and allow hyphen ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 01:56 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Regular expression to allow alphanumeric characters and hyphens
var regExp = /^[a-zA-Z0-9-]+$/;
// Check if the new value matches the regular expression and has a length of 32 characters
if (!regExp.test(newValue) || newValue.length != 32) {
alert("Please enter an alphanumeric value with hyphens allowed, and ensure it is exactly 32 characters long.");
g_form.clearValue('description');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 01:47 AM
Hi @sony8
Below script can be used to fulfill your requirement.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regExp = /^[a-zA-Z0-9-]{32}$/;
if (!regExp.test(newValue)) {
alert("Please Enter alpha-numeric value only");
g_form.clearValue('short_description');
}
}
Explanation:
- ^ asserts the position at the start of the string.
- [a-zA-Z0-9-] matches any alphanumeric character or a hyphen.
- {32} ensures that the previous character set is matched exactly 32 times.
- $ asserts the position at the end of the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 01:56 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Regular expression to allow alphanumeric characters and hyphens
var regExp = /^[a-zA-Z0-9-]+$/;
// Check if the new value matches the regular expression and has a length of 32 characters
if (!regExp.test(newValue) || newValue.length != 32) {
alert("Please enter an alphanumeric value with hyphens allowed, and ensure it is exactly 32 characters long.");
g_form.clearValue('description');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader