- 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-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-13-2023 02:34 AM
Hi @DYCM,
That has actually worked, foolish of me using the incorrect field name.
Thank you all for the help.