- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 07:07 AM
Hi,
I have a requirement where i should not be able to enter some special characters. I tried it but it is not working. Kindly help. Please don't post links.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^[~#%&*{}:<>?/|"\–]*$/;
if(!pattern.test(newValue)){
g_form.addErrorMessage('The following characters cannot be entered' + ' ~, #, %, & , *, {, }, \, :, <, >, ?, /, |, ", –');
g_form.clearValue('outlook_distribution_group_list_name');
}
}
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 08:18 AM
try this and it will only allow a to z, A to Z and numbers
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('outlook_distribution_group_list_name');
// Regular expression to allow only alphabets and numbers
var regex = /^[a-zA-Z0-9]+$/;
if (!regex.test(newValue)) {
// Display an error message if the input is invalid
g_form.showFieldMsg('outlook_distribution_group_list_name', 'Only alphabets and numbers are allowed.', 'error');
// Clear the invalid input
g_form.clearValue('outlook_distribution_group_list_name');
}
}
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-18-2025 08:19 AM
Hi @J Siva , @Ankur Bawiskar , @GopikaP
I simply used this and it works.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^[a-zA-Z0-9-.,]*$/;
if(!pattern.test(newValue)){
g_form.addErrorMessage('Special characters cannot be entered');
g_form.setValue('outlook_distribution_group_list_name', '');
}
}
Regards
Suman P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 07:23 AM
Hi @Dave_p , Can you try this one?
var pattern = /^[^~#%&*{}\\:<>\?/|\"–]*$/;
if(!pattern.test(newValue)){
g_form.addErrorMessage('The following characters cannot be entered' + ' ~, #, %, & , *, {, }, \, :, <, >, ?, /, |, ", –');
g_form.clearValue('outlook_distribution_group_list_name');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 07:29 AM - edited 03-18-2025 07:36 AM
Hi @Dave_p
You can achieve this via variable regex.
Regex: ^[a-zA-Z0-9\s]+$
Step1: Create variable validation regex
Step2: Select that under "Type specifications" section validation regex field.
Hope this helps.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 08:18 AM
try this and it will only allow a to z, A to Z and numbers
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('outlook_distribution_group_list_name');
// Regular expression to allow only alphabets and numbers
var regex = /^[a-zA-Z0-9]+$/;
if (!regex.test(newValue)) {
// Display an error message if the input is invalid
g_form.showFieldMsg('outlook_distribution_group_list_name', 'Only alphabets and numbers are allowed.', 'error');
// Clear the invalid input
g_form.clearValue('outlook_distribution_group_list_name');
}
}
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-18-2025 08:27 AM
Seems you marked your own response as correct.
Remember marking your own response as correct doesn't add up to the correct answer count for user profile.
Thank you for marking my response as helpful.
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