How convert single line text to decimal for 4 variables in one client script

Raveendra7
Tera Contributor

Hi 

I have a requirement

 

i have 4 variables

Questionstype 
HeightDecimal (0.000)
WidthDecimal (0.000)
LengthDecimal (0.000)
weightDecimal (0.000)

 

How to convert single line text variables into Decimal in one client script.

 

and when the user entered not decimal show a filed error as "please enter decimal value 0.000"

4 REPLIES 4

Samaksh Wani
Giga Sage
Giga Sage

Hello @Raveendra7 

 

https://www.servicenow.com/community/itsm-forum/how-do-i-make-a-single-line-text-field-to-a-number-f...

 

 

var regex = (\.[0]{1,4})?$ ;

if(!regex.test(newValue)){
gs.info('your message')
}

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

Hi @Samaksh Wani 

i want this script work for remaining 3 variables in single script

g_form.getValue('Width');

g_form.getValue('Lenght');

g_form.getValue('weight');   

 

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

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

          return;

    }

  var myVal = g_form.getValue('Height');

  var matchPattern = /^\d{4}[.]\d{2}$/;

  var myResult = matchPattern.test(myVal);

  if(!myResult){

  g_form.clearValue('Height');

  g_form.showFieldMsg('Height','Required format is xxxx.xx','error');

  }}

 

Hello @Raveendra7 

 

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

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

          return;

    }

  var myH = g_form.getControl('Height');

  var myW = g_form.getControl('Width');

  var myL = g_form.getControl('Length');

  var matchPattern = /^\d{4}[.]\d{2}$/;

  if(myH.changed){

  if(!myH.test(matchPattern)){

  g_form.clearValue('Height');

  g_form.showFieldMsg('Height','Required format is xxxx.xx','error');

  }

  }

 if(myL.changed){

  if(!myL.test(matchPattern){

  g_form.clearValue('Length');

  g_form.showFieldMsg('Length,'Required format is xxxx.xx','error');

  }

}

 if(myW.changed)

  if(!myW.test(matchPattern){

  g_form.clearValue('Width');

  g_form.showFieldMsg('Width,'Required format is xxxx.xx','error');

  }

 

}

 

 

Samaksh Wani
Giga Sage
Giga Sage

Hello @Raveendra7 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh