- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 03:31 PM - edited 12-14-2023 02:07 AM
Hi everyone,
I am getting acquaintance with Service Now Development and I have noticed that there are a few limitations with client-scripts. I am trying to create a minimum character requirement for the field additional comments but I am running into issues where the form does not allow submission after the first return false statement.
Would anyone have a solution on how I could achieve this solution to both Web and Portal?
function onSubmit() {
//Minimum number of characters
var charLength = 50;
do {
//Get value from form
var currentValue = g_form.getValue('additional_comments');
//Check if the value is less than 50 characters
if (currentValue.length < charLength) {
g_form.addErrorMessage("Incident description must be at least 50 characters long.");
return false;
} else {
//If the character length is higher than or equal to 50, remove the error message
g_form.clearMessages();
}
} while (currentValue.length < charLength);
//Allow form submission
return true;
}
I am using the do-loop to continuously check the length and display an error until the user enters a value with at least 50 characters without asking the user to refresh the page. However, I am not sure if Service Now has limitations regarding this approach.
The results are:
I really appreciate in advance for any tips or guidance regarding this question.
Regards,
Giovanni
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 02:20 AM
Hi @Gioo ,
You can use onSubmit to check your input value before submitting the form, this is fine. I think the issue you are facing is caused by the field name. Instead of using "additional_comments", I think you meant to use "comments".
Please see the demo for you
function onSubmit() {
var comments = g_form.getValue("comments");
if (comments.length < 10) {
g_form.addInfoMessage(comments);
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 03:38 PM - edited 12-12-2023 03:38 PM
I think you can simplify your script as below
function onSubmit() {
g_form.clearMessages();
//Minimum number of characters
var charLength = 50;
//Get value from form
var currentValue = g_form.getValue('additional_comments');
//Check if the value is less than 50 characters
if (currentValue.length < charLength) {
g_form.addErrorMessage("Incident description must be at least 50 characters long.");
return false;
}
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 12:50 AM
Hi @SanjivMeher,
Thank you for your input,
I had a similar script to yours before and once the return false is triggered, the form submit stops working. What I am trying to achieve is a script that will do the following:
1. If the user writes less than 50 characters and click submit
- An error message warns the user to enter at least 50 characters to the field
- Do not allow the user to submit the form (return false)
2. Without refreshing the page, if the user writes more than 50 characters and click submit
- Clear the error message
- Allow the user to submit the form because the criteria was meet (50 characters or more).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 01:27 AM
rather than handling this in onSubmit why not use onChange validation and show error message?
I would recommend onchange since it makes sense pointing the error when user types in rather than allowing user to enter less than 50 chars and then doing the validation onSubmit
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
12-13-2023 01:43 AM - edited 12-13-2023 01:57 AM
Hi @Ankur Bawiskar,
I was about to include more information to my latest reply and just saw your message coming in.
I have tried the script onChange and I can't achieve the end result which is to stop the users from submitting the form with less than 50 characters. The only type of script that seems to stop the form is onSubmit but then that does not allow to do more attempts after it.
This is my script for onChange:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading){
return;
}
//Minimum number of characters
var charLength = 50;
//If the characters are less than 50, display the message
if (newValue.length < charLength) {
g_form.addErrorMessage("Incidents description must be at least 50 characters long.");
return false;
}
//If the character length is higher than 50, remove the error message
if (newValue.length >= charLength) {
g_form.clearMessages();
}
}
Thank you for your input.