Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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;
    }

}

 

Hi @DYCM,

 

That has actually worked, foolish of me using the incorrect field name.

 

Thank you all for the help.