The CreatorCon Call for Content is officially open! Get started here.

Is there any way to make a string field of at least 40 character or we have configure through script

1_DipikaD
Kilo Sage

Hi All,

 

I want to make character length of a string field of at least 40 character . Is there any way configure it without scripting or we have to approach through script ?

 

Thank You

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

on string field in dictionary you can use max_length to allow max characters

but nothing OOTB is present for minimum length

you will require onChange client script for this.

AnkurBawiskar_0-1759471075625.png

 

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

View solution in original post

mayankkumar
Kilo Patron
Kilo Patron

Hi @1_DipikaD,
You can set the maximum character length in the Dictionary for that field. However, if you also want to ensure the field contains at least 40 characters, you’ll need to add an onChange client script on field to enforce that.
----------------------------------------------------------------------------------------------------------------------------------------------
Please mark my response helpful and accept as solution
Thanks & Regards
Mayank

View solution in original post

14 REPLIES 14

 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var minLength = 60;
   var lifecycleState = g_form.getValue("u_lifecycle_state");
    if (typeof newValue === "string" && newValue.length < minLength) {
        g_form.showErrorBox('u_build_state_justification', 'The field must contain at least ' + minLength + ' characters.');
        control.setValue(oldValue);
        return false;
    } else {
       
        g_form.hideErrorBox('u_build_state_justification');
        return true;
    }


   //Type appropriate comment here, and begin script below
   
}
 

 

@1_DipikaD 

user enters some text, your script validates the length

if length is less than 60 you show error box and clear the field

why are you setting the value again in that field? let user type in fresh

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var minLength = 60;
    if (newValue.length < minLength) {
        g_form.showErrorBox('u_build_state_justification', 'The field must contain at least ' + minLength + ' characters.');

    } else {
        g_form.hideErrorBox('u_build_state_justification');
    }
    //Type appropriate comment here, and begin script below

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 

Can we make the same thing on 'onsubmit' client script ? With changing of oldValue ?

I mean while this error appears it should not allow the form to be submitted .

@1_DipikaD 

yes you can throw error near field using onSubmit client script as well

like this

when error box is shown near field then form cannot be submitted and no need to use return false

function onSubmit() {

    var val = g_form.getValue('u_build_state_justification');
    var minLength = 60;
    if (val.length < minLength) {
        g_form.showErrorBox('u_build_state_justification', 'The field must contain at least ' + minLength + ' characters.');
    } else {
        g_form.hideErrorBox('u_build_state_justification');
    }
    //Type appropriate comment here, and begin script below

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

 

Is it ok if I try the below script ?  But the new value is getting saved even it is less than 60 character , ideally it should not allow to submit ? I just want to through this error while the field value changes and should not allow to submit the form if condition satisfies . 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var minLength = 60;
    if (newValue.length < minLength) {
        g_form.showErrorBox('', 'The field must contain at least ' + minLength + ' characters.');
        g_form.submit = false;

    } else {
        g_form.hideErrorBox('');
        g_form.submit = true;
    }
    //Type appropriate comment here, and begin script below
   
   
}