Field Validation in Record Producer

salu
Mega Guru

ctomasi

b-rad

larstange

Hello,

I want to have field level validation in   Record Producer

for e.g.,

My short description field will only take 80 character,

When it exceeding above that 80 character,it should not allow it.

Below code will work   in CMS, but in Service Portal it won't work   as 'getcontol' methodn won't support it mobile.

Can some one give an input on this?

function onLoad() {

    //Use the 'onkeyup' event to check the 'isMaxLength' function

    //Get the control

    var control = g_form.getControl('short_description');

  alert(control);     //Set its onkeyup method

    control.onkeyup = isMaxLength;

}

function isMaxLength(){

    var mLength=85;

    var control = g_form.getControl('short_description');

  alert(control);

    if (control.value.length > mLength){

          g_form.hideErrorBox('short_description');

          g_form.showErrorBox('short_description', 'You have reached the maximum character limit for this field.');

          control.value=control.value.substring(0,mLength);

    }

    else{

          g_form.hideErrorBox('short_description');

    }

}

find_real_file.png

4 REPLIES 4

larstange
Mega Sage

Hi



you can just add the max_length = 80 parameter to the   Variables attributes field



Variable Types - ServiceNow Wiki


Hello Lars,


Thanks.It was very helpful.




I have wrote an client script on change of short_description. I want to show an message to user to know they have reached the limit



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



if(newValue.length>80)


{


  g_form.addErrorMessage('You have reached the limit');



}


    //Type appropriate comment here, and begin script below


   


}


The above code is not working.


larstange
Mega Sage

Hi you have applied the max_length attribute you will not get above the limit. The code should be



if(newValue.length == 80)


{


  g_form.addErrorMessage('You have reached the limit');



}