client script to check string length and restrict user

sry
Giga Guru

Hi all,

                    we have a requirement to restrict user not to enter more than 100 characters for a field of type string. currently the field length is 120 but we don't want user to enter more than 100. For this i am thinking a "onChange" client script, i am not sure onChange is correct. Could you please guys help me how to write a clientscript to restrict the user by displaying an error beside the field to correct the length of the message he typed to 100 based on condition of assignedto=ABC,assignment group=XYZ.

Thanks,

Sry

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Sry,



Sample script-Untested code. Adjust as per your req.


var assignedto = g_form.getDisplayBox('assigned_to').value;


var assignmengrp = g_form.getDisplayBox('assignment_group').value;


var field = g_form.getValue('short_description').length;


if(assignedto == 'XXX' && assignmengrp == 'XXX' && field>100)


  {


  alert('YOUR TEXT');


  return false;


}


venkatiyer1
Giga Guru

Hi Sry,



If it is a field of type string the dictionary form has a field called Max Length where you can enter the number of characters. This is obviously different to the width of the field which could be larger.



If it is a variable of Single line text you can add a attribute called max_length



If either of the above doesnt fit your requirement, there is a blog post below which details a client script aiding in achieving the same.


Setting the Maximum Length for a String Variable