Minimum character onChange for Client Script

Gioo
Tera Expert

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:

 

IncidentDescription.pngIncidentDescription2.png

 

I really appreciate in advance for any tips or guidance regarding this question.

 

Regards,

Giovanni

1 ACCEPTED SOLUTION

DYCM
Mega Sage

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

1.png

 

function onSubmit() {

    var comments = g_form.getValue("comments");
    if (comments.length < 10) {
        g_form.addInfoMessage(comments);
        return false;
    }

}

 

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

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.

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

  1. An error message warns the user to enter at least 50 characters to the field
  2. 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

  1. Clear the error message
  2. Allow the user to submit the form because the criteria was meet (50 characters or more).

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Gioo 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.