- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 11:21 PM
Hello Team,
I have implemented the validation for the update set naming convention, and everything is working fine. However, when a user submits the form with a name that does not follow the convention, the validation message appears but disappears within a second.
I would like the message to remain visible for at least 10 seconds to ensure the user has enough time to read it. How can I achieve this?
I have attached my onLoad and onSubmit client scripts for reference.
Onload Client Script:
function onLoad() {
var fn=g_user.firstName;
var ln=g_user.lastName;
var fv=fn.charAt(0);
var fv2=ln.charAt(0);
if(g_form.isNewRecord())
{
g_form.setValue('name',"PN-"+fv+fv2+"-"+"STR");
}
}
onSubmit client script
function onSubmit() {
var name = g_form.getValue('name');
alert("Onsubmit"+name);
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern =/^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
location.reload();
return false;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 01:05 AM
Hi @Mark Wood
Then just use the same logic in the OnSubmit client script instead of reloading the form.
function onSubmit() {
var name = g_form.getValue('name');
alert("Onsubmit" + name);
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
setTimeout(function() {
g_form.clearValue("name");
g_form.clearMessages();
var fn = g_user.firstName;
var ln = g_user.lastName;
var fv = fn.charAt(0);
var fv2 = ln.charAt(0);
if (g_form.isNewRecord()) {
g_form.setValue('name', "PN-" + fv + fv2 + "-" + "STR");
}
}, 10000);
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 11:40 PM
Hi @Mark Wood ,
try set timeout function in on submit client script as mentioned below:
function onSubmit() {
var name = g_form.getValue('name');
alert("Onsubmit"+name);
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern =/^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
setTimeout(function() {
gForm.clearMessages();
}, 10000);
location.reload();
return false;
]
}
}
If this solution helps you then, mark it as accepted solution ✔️ and give thumbs up 👍 !
Regards,
Pratik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 12:05 AM
I see the issue you're facing with the error message disappearing too quickly and the subsequent reload prompt. We can handle this more smoothly without forcing a page reload, which leads to that annoying popup.
Here's how you can tweak your onSubmit client script to show the error message for 10 seconds without triggering a page reload:
function onSubmit() {
var name = g_form.getValue('name');
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story (e.g., FN-MI-STR00011)");
// Prevent form submission and allow the message to stay for 10 seconds
setTimeout(function() {
// Simply wait for 10 seconds to let the user read the message
}, 10000);
return false; // Stop the form from submitting
}
}
This script prevents the form from submitting if the name doesn’t match the pattern and keeps the error message visible for 10 seconds. By not reloading the page, we avoid that browser warning about unsaved changes.
But if you really need to refresh the form after showing the message, you can do it without navigating away, like this:
function onSubmit() {
var name = g_form.getValue('name');
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story (e.g., FN-MI-STR00011)");
// Show the error message for 10 seconds, then refresh the form
setTimeout(function() {
g_form.clearMessages(); // Clear the error message after 10 seconds
g_form.load(); // Reload the form without a full page reload
}, 10000);
return false; // Stop the form from submitting
}
}
With this approach, the form will refresh without the user seeing the reload confirmation dialog. We first clear the error messages, then reload just the form itself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 12:53 AM
Hello @johndoh ,
We need to reload the entire page because the onload client script is executing and setting the field value.
Please review the onload client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 01:05 AM
Hi @Mark Wood
Then just use the same logic in the OnSubmit client script instead of reloading the form.
function onSubmit() {
var name = g_form.getValue('name');
alert("Onsubmit" + name);
// Define the required pattern: <Project Prefix>-<User Initials>-<Story>
var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
if (!pattern.test(name)) {
g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
setTimeout(function() {
g_form.clearValue("name");
g_form.clearMessages();
var fn = g_user.firstName;
var ln = g_user.lastName;
var fv = fn.charAt(0);
var fv2 = ln.charAt(0);
if (g_form.isNewRecord()) {
g_form.setValue('name', "PN-" + fv + fv2 + "-" + "STR");
}
}, 10000);
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 12:23 AM
why not use before insert/update business rule and show error message?
in that way user will have to close the message from his/her side.
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