You are viewing limited content. For full access, please sign in.

Question

Question

Set decimals for number fields (forms 9.2)

asked on October 3, 2017

Hi all, 

The function below works great in doing my calculation.  However I get and 'invalid input' message when a decimal number is entered in either field.  Is there a way I can set mileage to (2) decimal places and rate to (3) decimal places?  

 

	function sumtotal() {
		var sum = 0;
		$('.cf-table-block tbody tr').each(function () {
          	var s = 0.0;
			s = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());
          	s = Math.round(s * 100)/100.0; //round to two decimal places
            $(this).find('.subtotal input').val(s);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
            			
		})

 

0 0

Replies

replied on October 3, 2017

Hi Ramika,

 

Normally to set a fixed decimal place count, you should be able to use the "toFixed()" function. if you wanted 2 decimal places you could try ... "parseFloat($(this).find('.mileage input').val().toFixed(2));". Alternatively you could get the input in one variable and set the decimal places in another variable.

Does your input have 2 decimal places already? Or are you wanting the value of 's' to be rounded to 2 decimal places after your calculation?

I'm not sure if this will fix your issue exactly though. One thing I like to do when troubleshooting is logging values into console at various points and then viewing the developer console to see what may be causing the issues.

 

Regards,

Aaron

0 0
replied on October 4, 2017

im looking to be able to enter a decimal number for rate & mileage.  Rate and mileage are form fields.  

0 0
replied on October 5, 2017

When I enter the decimal in the form field I get the error "invalid input"  I am looking for a way to fix this issue.  

0 0
replied on October 5, 2017

Hi Ramika,

Are there any formatting restrictions on the field? Are you able to take some screenshots so I and others can better understand the issue?

 

Regards,

 

Aaron

0 0
replied on October 5, 2017

0 0
replied on October 5, 2017

Thanks Ramika,

I understand what you're saying now. There is an option in 10.2.1 where you can set whether to use decimals or not in the number fields. Are you able to upgrade at all? 

Otherwise you can try add this to your javascript disabling the validation on those fields- 

 $('.milage input').addClass('lf-no-validate');

 $('.rate input').addClass('lf-no-validate');

Regards,

Aaron

0 0
replied on October 5, 2017

I'll give it a try and let you know.  

 

I anticipate updating over the Christmas break.  I've heard good things.  :-)

0 0
replied on October 9, 2017

I tried the above example posted.  It didn't work.  

0 0
replied on October 9, 2017

Hi Ramika, 

Where did you put the javascript? outside or inside the function? It should be the first thing after document ready i.e.

$(document).ready(function(){

$('.milage input').addClass('lf-no-validate');

 $('.rate input').addClass('lf-no-validate');

function sumtotal(){.........

 

If you did do the javascript like this, it might not be supported for that version of forms. Sorry, I haven't got 9.2 to test with to give a proper answer on this. Could you change the fields to single line fields instead of number fields until you update?

 

Regards,

Aaron

0 0
replied on October 10, 2017

I've tried it in the location you specified and I do not get the message.  however, it does not calculate.  If I comment this section out it displays 'invalid input' and it does the calculation.  

0 0
replied on October 10, 2017

Hi Ramika,

It looks like I made a typo in mileage. Are you able to update the css classname in the javascript and see? What does the browsers Developer console say when you load the form? If you update the css classname in the javascript and it still does not work, can you post all of your javascript code please?

 

Regards,

Aaron

0 0
replied on October 11, 2017 Show version history

I uploaded the complete code: 

$(document).ready(function() {
    //$('.mileage').addClass("lf-no-validate");
	//$('.rate').addClass("lf-no-validate");
  	$('.cf-table-block').on('blur', 'input', sumtotal);
	$('.tax').on('blur', 'input', sumtotal);
	$('.shipping').on('blur', 'input', sumtotal);
    //$('.mileage').addClass("lf-no-validate");
	//$('.rate').addClass("lf-no-validate");
	function sumtotal() {
		var sum = 0;
		$('.cf-table-block tbody tr').each(function () {
            $('.mileage').addClass("lf-no-validate");
			$('.rate').addClass("lf-no-validate");
          	var s = 0.0;
			s = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());
          	s = Math.round(s * 100)/100.0; //round to two decimal places
            $(this).find('.subtotal input').val(s);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
            			
		});
			$('.total input').val(sum);
	}
	function parseNumber(value) {
		var f = parseFloat(value); //Convert to float number
		return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

 

0 0
replied on October 11, 2017

Hi Ramika,

Can you change it to the following please?

$(document).ready(function() {

    $('.mileage input').addClass('lf-no-validate');

    $('.rate input').addClass('lf-no-validate');

    $('.cf-table-block').on('blur', 'input', sumtotal);

    $('.tax').on('blur', 'input', sumtotal);

    $('.shipping').on('blur', 'input', sumtotal);


    function sumtotal() {

        var sum = 0;

        $('.cf-table-block tbody tr').each(function () {

            var s = 0.0;

            s = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());

            s = Math.round(s * 100)/100.0; //round to two decimal places

            $(this).find('.subtotal input').val(s);

            sum += s;

            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());

                         

        });

            $('.total input').val(sum);

    }

    function parseNumber(value) {

        var f = parseFloat(value); //Convert to float number

        return isNaN(f) ? 0 : f; //treat invalid input as 0;

    }

});

 

0 0
replied on October 12, 2017

"Invalid Input"

$(document).ready(function() {
    $('.mileage').addClass('lf-no-validate');
	$('.rate').addClass('lf-no-validate');
  	$('.cf-table-block').on('blur', 'input', sumtotal);
	$('.tax').on('blur', 'input', sumtotal);
	$('.shipping').on('blur', 'input', sumtotal);
  
	function sumtotal() {
		var sum = 0;
		$('.cf-table-block tbody tr').each(function () {
          	var s = 0.0;
			s = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());
          	s = Math.round(s * 100)/100.0; //round to two decimal places
            $(this).find('.subtotal input').val(s);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
            			
		});
			$('.total input').val(sum);
	}
	function parseNumber(value) {
		var f = parseFloat(value); //Convert to float number
		return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

 

0 0
replied on October 12, 2017

Hi Ramika, 

Have you changed the '.mileage input' to '.mileage'? In both examples you posted above, the lf-no-validate lines do not contain the correct class identifier? from the tests that I was doing in my environment, it was working when I had the proper css class selecter of '.classname input'

0 0
replied on October 16, 2017

so you are setting the css class to input and not the name of the variable?

0 0
replied on October 16, 2017 Show version history

Hi Ramika,

 

That is correct.

 

I have had another look at this as well for you. Maybe a better way to tackle this is changing the pattern LF is looking for. This should be available by adding the following:

  $('.mileage input').attr('pattern','\\d{1,9}?([.]\\d{1,2})?');
  $('.rate input').attr('pattern','\\d{1,9}?([.]\\d{1,3})?');

The idea here is that you can input any whole number that is up to 9 digits long with an optional following decimal number. The mileage pattern will limit it to 2 decimal places and rate to 3 as requested initially.

Replace both of the above with this instead and see how it goes. Sorry I hadn't thought of this earlier, I was just trying to disable the validation entirely.

 

I have attached my samples that I was working with for your reference.

Note: I have had to do my testing here on Single Line Fields and not Number fields. It might be easier for you to change the same within your setup if you haven't done already? That way you could just add the regular expression of \d{1,9}?([.]\d{1,2})? for mileage and \d{1,9}?([.]\d{1,3})? for rate and everything else should still work the same?

 

Regards,

 

Aaron

ElementInDevConsole.PNG
CodeInJsEditor.PNG
PatternMatchWorking1.PNG
PatternMatchWorking2.PNG
0 0
replied on October 17, 2017

Hi and thank you for the details... interesting, it works for 'rate' but still 'invalid input' for 'mileage'

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.