Want to put character limit on a variable on a catalog item

Charles Hong
Mega Expert

Hi - i have a variable on a catalog item that is a singe line text field. 

I want to do a onSubmit catalog client script so that users can't put more than 15 characters in the field. I have attempted w/ the script below and am unable to have it work successfully. If someone could please correct my script. 

 


function onSubmit() {

var length = 15;
var name = g_form.getValue('hostname'); 

               
 if(name.length > length)
                             alert('Please ensure the hostname is not more than 15 characters') ;          


   }

5 REPLIES 5

Geoff_T
Mega Sage

Why not use the max_length variable attribute instead:

https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/product/service-catalog-management/reference/variable-attributes.html

 

max_lengthSets the maximum number of characters allowed in the field. By default, the field accepts long strings of text, several thousand characters. Set the max_length attribute as appropriate for the information the variable is collecting. For example, to allow for entry of an address, set max_length=200, or other appropriate length.

Applicable variables: Single-line text, Wide single-line text

 

Geoff

Yousaf
Giga Sage

Hi,

 please try 

var str = newValue; // var str = g_form.getValue('variable_name')
var len = str.length;
if(len> 10 && len< 5)
{
g_form.setValue('variable_name','');
alert('Please enter atleast 5 and maximum of 10');
}


Change the variables name please.


Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

var name = g_form.getValue('hostname');
var len = name.length;
if(len> 15) 
{
g_form.setValue('hostname','');
alert('Please ensure the hostname is not more than 15 characters');
}


***Mark Correct or Helpful if it helps.***

Also you can Click on configure variable. Go to default Value tab > in variable attribute field add below.

max_length=15 


***Mark Correct or Helpful if it helps.***