Script on a field that converts any number to currency and checks to make sure it's a certain amount and need it to allow commas

carlh
Kilo Guru

Hello   abhi_r

I tried the script you helped me revise and the results are the same.

I entered in 130,000 and it throws up the box saying "It is not a valid number" and then clears it out completely.

So I needed the following:

Script on a field that would pop up a warning if the amount was under $1mil - Working

The same field to convert to for as follows: $1,000,000.00

And for it to be able to accept commas ","     - Our data is being passed with commas   - Example 130,000 so that error I get would be thrown every time.

Here's the actual script I used.

function onLoad() {

  Event.observe($('ast_contract.u_coverageamt_string'), 'change', function() {

  var varHtml=$('ast_contract.u_coverageamt_string');

  if(varHtml.value!=''){

  if(!(/(^\d*\.\d*$)|(^\d*$)/g.test(varHtml.value))&& !(/(^\$\d*\.\d*$)|(^\$\d*$)/g).test(varHtml.value)){

  alert("It is not a valid number");

  g_form.setValue('u_coverageamt_string','');

  }

  else{

  var formatted_value='';

  if(varHtml.value.indexOf('.')>-1){

  if(varHtml.value.indexOf('$')>-1){

  formatted_value=varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];

  g_form.setValue('u_coverageamt_string',formatted_value);

  }

  else{

  formatted_value="$"+varHtml.value.split('.')[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];

  g_form.setValue('u_coverageamt_string',formatted_value);

  }

  }

  else{

  if(varHtml.value.indexOf('$')>-1){

  formatted_value=varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';

  g_form.setValue('u_coverageamt_string',formatted_value);

  }

  else{

  formatted_value="$"+varHtml.value.replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';

  g_form.setValue('u_coverageamt_string',formatted_value);

  }

  }

  if((parseFloat(g_form.getValue('u_coverageamt_string').split('.')[0].replace(/\D/g, ""))<1000000)){

  alert("Insurance amount is less than $1 million. Please ensure all appropriate documentation/exemptions is uploaded and/or vendor has Umbrella insurance $1 million or more.");

  }

  }

  }

  });

}

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here is the complete script.


function onLoad() {


  Event.observe($('incident.u_custom_currency'), 'change', function() {


  var varHtml=$('incident.u_custom_currency');


  if(varHtml.value!=''){


  if(!(/(^\d*\.\d*$)|(^\d*$)/g.test(varHtml.value.replace(/,/g,'')))&& !(/(^\$\d*\.\d*$)|(^\$\d*$)/g).test(varHtml.value.replace(/,/g,''))){


  alert("It is not a valid number");


  g_form.setValue('u_custom_currency','');


  }


  else{


  var formatted_value='';


  if(varHtml.value.indexOf('.')>-1){


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);



  }


  else{


  formatted_value="$"+varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  else{


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  else{


  formatted_value="$"+varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  if((parseFloat(g_form.getValue('u_custom_currency').split('.')[0].replace(/\D/g, ""))<1000000)){


  alert("Coverage is not enough");


  }


  }


  }


  });


}


View solution in original post

8 REPLIES 8

Abhinay Erra
Giga Sage

Well, your code is not in sync with the code I posted.



function onLoad() {


  Event.observe($('incident.u_custom_currency'), 'change', function() {


  var varHtml=$('incident.u_custom_currency');


  if(varHtml.value!=''){


  if(!(/(^\d*\.\d*$)|(^\d*$)/g.test(varHtml.value.replace(/,/g,'')))&& !(/(^\$\d*\.\d*$)|(^\$\d*$)/g).test(varHtml.value.replace(/,/g,''))){


  alert("It is not a valid number");


  g_form.setValue('u_custom_currency','');


  }


  else{


  var formatted_value='';


  if(varHtml.value.indexOf('.')>-1){


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);



  }


  else{


  formatted_value="$"+varHtml.value.split('.')[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  else{


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  else{


  formatted_value="$"+varHtml.value.replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  if((parseFloat(g_form.getValue('u_custom_currency').split('.')[0].replace(/\D/g, ""))<1000000)){


  alert("Coverage is not enough");


  }


  }


  }


  });


}


How can do same thing on my catalog item onchange client script. my field name is "cost"  please help me.

 

When user enter any value on the cost field it should be convert as currency. 

Example : 764537 convert as $7,64,537.00 

Abhinay Erra
Giga Sage

Here is the complete script.


function onLoad() {


  Event.observe($('incident.u_custom_currency'), 'change', function() {


  var varHtml=$('incident.u_custom_currency');


  if(varHtml.value!=''){


  if(!(/(^\d*\.\d*$)|(^\d*$)/g.test(varHtml.value.replace(/,/g,'')))&& !(/(^\$\d*\.\d*$)|(^\$\d*$)/g).test(varHtml.value.replace(/,/g,''))){


  alert("It is not a valid number");


  g_form.setValue('u_custom_currency','');


  }


  else{


  var formatted_value='';


  if(varHtml.value.indexOf('.')>-1){


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);



  }


  else{


  formatted_value="$"+varHtml.value.split('.')[0].replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.'+varHtml.value.split('.')[1];


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  else{


  if(varHtml.value.indexOf('$')>-1){


  formatted_value=varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  else{


  formatted_value="$"+varHtml.value.replace(/,/g,'').replace(/(\d)(?=(\d{3})+$)/g, '$1,')+'.00';


  g_form.setValue('u_custom_currency',formatted_value);


  }


  }


  if((parseFloat(g_form.getValue('u_custom_currency').split('.')[0].replace(/\D/g, ""))<1000000)){


  alert("Coverage is not enough");


  }


  }


  }


  });


}


Thank you again! you have been awesome in helping me these last few days.



abhi_r