
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 08:29 PM
I have a requirement to be implemented for a field. For which I need to implement onChange script with below validation:
Alphanumeric combination of 14 digits where the value should start with "A" and only "-" is allowed as special character.
Please help me in implementing this requirement.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 09:34 PM - edited 09-20-2023 09:54 PM
Hello @Deepika Mishra
Please try below code segment
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regex = /^A[A-Za-z0-9-]{13}$/;
var name = g_form.getValue('u_name');
if(regex.test(name) == false){
g_form.showFieldMsg('u_name','String should start with A and must contain 14 digits, Hyper (-) only allowed','error');
}
}
Please mark my answer helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 08:39 PM
should be an easy with regular expression.
you can search related to this on google or regex sites
what did you try so far and what didn't work?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 09:11 PM
I want to how the combination of condition should be
I need help me in that, taking out regex is not big thing but extracting "-" and adding condition to it is.
So there I need help me in understanding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 09:34 PM - edited 09-20-2023 09:54 PM
Hello @Deepika Mishra
Please try below code segment
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regex = /^A[A-Za-z0-9-]{13}$/;
var name = g_form.getValue('u_name');
if(regex.test(name) == false){
g_form.showFieldMsg('u_name','String should start with A and must contain 14 digits, Hyper (-) only allowed','error');
}
}
Please mark my answer helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 09:37 PM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^A[A-Za-z0-9]{12}-[A-Za-z0-9]$/;
if (!pattern.test(newValue)) {
g_form.showFieldMsg(control.name, "Value should start with 'A' and be 14 characters long (including '-')", "error");
g_form.setValue(control.name, '', '');
} else {
g_form.hideFieldMsg(control.name);
}
}