Show commas in numeric value in the service catalog

Marcel H_
Tera Guru

I have a service catalog item that has a field used to enter a number, which is then mapped to a Price field on the task form. I've seen a few posts on enforcing characters and requiring users to enter a dollar amount in the correct format, which is fine, but what I'm being asked to do is the following:

Validate that no letters or characters not normally associated with US currency are allowed (0-9 $ , .)

Format the number as it is typed to automatically insert commas at the correct intervals (eg. 1,000     10,000     100,000 etc.)

Automatic insertion of the comma is to help prevent user error when they type something like 25000 quickly but meant to type 250000. Trailing zeros can make it difficult to spot errors, apparently

Not sure if there is really a way to do this. Would be nice to have an Integer type field for the service catalog.

1 ACCEPTED SOLUTION

Hi Marcel,



You can try below script :


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



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


  return;


  }


  var result = formatNumber(newValue);


  if(result == false){


        alert('Only numeric values allowed');


        return;



  }


    // To avoid recusrion


  if(result != newValue){


      // Change u_field_name to the field on which this onChange script is applied


      g_form.setValue('u_field_name', formatNumber(newValue));



  }


}


function formatNumber(x) {


  // Regular expression allowing only numeric values


  var reg = /^[\d,]+$/;


  if(!x.match(reg))


      return false;


  else


      return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");   // Insert commas at correct position


  }




Let me know if this works fine


View solution in original post

14 REPLIES 14

Hi Marcel
Could you support me in sharing your regex? is that I have searched everywhere how to allow at least two decimal numbers and a dollar sign.

I hope I can count on your help.
Greetings.

Hi Mohammed,



I need to add ,00 automatically at the end of numbers. For example 250.343.343,00



I tried to combine script below with the code you shared, but i couldnt accomplish. Could you help, please?



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


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


          return;


      }


var curval = newValue.split(",")[1];


if(curval == undefined || curval.length != 2)


g_form.setValue('myfield',parseFloat(newValue).toFixed(2));


  // Type appropriate comment here, and begin script below


}


Hi Zee,

 

I have a variable cost, when I enter value in that it should take in commas, 

Example: when I enter 1000, it should display 1,000 while I am entering.

Can you please suggest me on that.

 

Thanks,

Uzma

I got but can you help me with one requirement. If we have different forms of currency like USD, EUR etc based on that commas should come. Can you help me with script?

Thanks,

Uzma

Hello bro,

 

Your script is very helpful. But can you please help me on this requirement? 

 

User needs to show comma on the numeric field same as above BUT also

 

  1. Require support 2 decimals. 
  2. The value could not be exceed 999,999,999.99