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.

Need to apply validation for set of fields using client script for checking length of each field in one go

Are Kaveri
Tera Contributor

 i have below requirement.

there are 3 ID fields where the length should not exceed 4000 characters and need to show error message when it is exceeding the limit.

I need to handle 3 fields in single validation by creating an array for above fields. can someone help me how to validate using client script.

Please help for minimising below script

code i written is on submit need to minimise below code .

var stud = g_form.getValue('student_id');
    if (stud.length != '' && stud.length > 4000) {

        g_form.showFieldMsg("student_id", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
        g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
        return false;

    }
    var stude = g_form.getValue('student_id1');
    if (stude.length != '' && stude.length > 4000) {

        g_form.showFieldMsg("student_id1", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
        g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
        return false;

    }
    var studen = g_form.getValue('student_id3');
    if (studen.length != '' && studen.length > 4000) {

        g_form.showFieldMsg("student_id3", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
        g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
        return false;

    }

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello,

Please check if the below code looks good to you and try once:

function onSubmit() {
    var isValid = true;
    var fieldNames = {
        "student_id": "Y",
        "student_id1": "Y",
        "student_id3": "Y"
    };
    for (var fname in fieldNames) {
        var fieldValue = g_form.getValue(fname);
        if (fieldValue && fieldValue.length > 4000) {
            g_form.showFieldMsg(fname, "This field has a maximum character limit of 4000. Please revise and submit.", "error");
            if (isValid)
                isValid = false;
        }
    }
    if (!isValid) {
        g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
        return false;
    }
}

With Array:

function onSubmit() {
    var isValid = true;
    var fieldNames = ["student_id","student_id1","student_id3"];
    for (var fname in fieldNames) {
        var fieldValue = g_form.getValue(fieldNames[fname]);
        if (fieldValue && fieldValue.length > 4000) {
            g_form.showFieldMsg(fieldNames[fname], "This field has a maximum character limit of 4000. Please revise and submit.", "error");
            if (isValid)
                isValid = false;
        }
    }
    if (!isValid) {
        g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
        return false;
    }
}

Please mark this as helpful/correct, if it answer your question.

Thanks

View solution in original post

8 REPLIES 8

Mahendra RC
Mega Sage

Hello,

Please check if the below code looks good to you and try once:

function onSubmit() {
    var isValid = true;
    var fieldNames = {
        "student_id": "Y",
        "student_id1": "Y",
        "student_id3": "Y"
    };
    for (var fname in fieldNames) {
        var fieldValue = g_form.getValue(fname);
        if (fieldValue && fieldValue.length > 4000) {
            g_form.showFieldMsg(fname, "This field has a maximum character limit of 4000. Please revise and submit.", "error");
            if (isValid)
                isValid = false;
        }
    }
    if (!isValid) {
        g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
        return false;
    }
}

With Array:

function onSubmit() {
    var isValid = true;
    var fieldNames = ["student_id","student_id1","student_id3"];
    for (var fname in fieldNames) {
        var fieldValue = g_form.getValue(fieldNames[fname]);
        if (fieldValue && fieldValue.length > 4000) {
            g_form.showFieldMsg(fieldNames[fname], "This field has a maximum character limit of 4000. Please revise and submit.", "error");
            if (isValid)
                isValid = false;
        }
    }
    if (!isValid) {
        g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
        return false;
    }
}

Please mark this as helpful/correct, if it answer your question.

Thanks

Thank you so much for your help 🙂

if i have to customise the error message

 g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");

instead i want array values which exceed more than 4000 character limit with comma separated value?

Error message :

You have exceeded the 4000 character limit + fieldValues

 

could you please help me for adding it in error message

Hi Mahendra,

Your script helped me as well. Thank you so much for that.

I have similar requirement with little change in error message.

For example - I have 4 fields a,b,c,d exceeding limit of 4000 characters. I need to show error message in comma separated way.

Error Message should be in below way

" You have exceeded the 4000 characters for fields a,b,c,d. Please revise and submit."

 

Can you please help me in achieving this.

 

Thanks in advance....

 

Thank you so much @Ankur Bawiskar   and @Mahendra  for your early response and solution from both were working fine...