0

I have an amount field but when the user tabs out the field, if they havn't added a decimal i want to add '.00' when they tab out the field.

Thing is i have no idea on how to do a check if it contains a '.'. I know how to add the '.00'

This is my code so far

function AddDecimalToAmounts()
{
    var ApproxAmount = $("#ApproximateValue_TextBox").val();
    var ApproxAmountVis = $('#chqAdditional_div').is(':visible');
    var UncryAmount = $("#UncryAmount_TextBox").val();
    var UncryAmountVis = $('#chqAdditional_div').is(':visible');

    if (ApproxAmountVis == true && UncryAmountVis== true)
    {
        if (//Code for if both amount fields are displayed and to add '.00' or not)
        {

        }
    }
    else if (ApproxAmountVis == false && UncryAmountVis== true)
    {
        if (//Code for if only the UncryAmount amount field is displayed and to add '.00' or not)
        {

        }
    }
    else if (ApproxAmountVis == true && UncryAmountVis== false)
    {
        if (//Code for if only the ApproxAmountVis amount field is displayed and to add '.00' or not)
        {

        }
    }
}
murday1983
  • 3,806
  • 14
  • 54
  • 105
  • You check if it contains a `.` the same way you check for any other string. What's the problem? Don't you know about `indexOf()`? – Barmar Sep 08 '16 at 14:59
  • `if (value.indexOf('.') == -1) { /* add the decimals */ }` – Rory McCrossan Sep 08 '16 at 14:59
  • In JavaScript, you can use indexOf to check if a string contains a character. – Sylvain B Sep 08 '16 at 14:59
  • 1
    @Barmar if he is asking is for something ..... he don't know how to do that , your comment seems a little rude – DaniP Sep 08 '16 at 15:01
  • Is that really the only change you want to make? What if they type `1.1`, wouldn't you like to change it to `1.10`? Just use `toFixed(2)` to give it 2 digits after the decimal no matter what the original was. – Barmar Sep 08 '16 at 15:01
  • Possible duplicate of [Check if a number has a decimal place/is a whole number](http://stackoverflow.com/questions/2304052/check-if-a-number-has-a-decimal-place-is-a-whole-number) – frhd Sep 08 '16 at 15:01

2 Answers2

1

Rather than check specifically if it has a decimal, you should just convert it to the number format that you want.

$("#ApproximateValue_TextBox").val(function(i, oldval) {
    if (oldval != '') { // Only if they filled in the field
        var value = parseFloat(oldval);
        if (isNaN(value)) { // If it's not a valid number, leave it alone
            return value;
        } else {
            return value.toFixed(2); // Convert it to 2 digits after decimal
        }
    } else {
        return '';
    }
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can simply do like this.

 var ApproxAmount = $("#ApproximateValue_TextBox").val();

if(parseFloat(ApproxAmount) == parseInt(ApproxAmount)){
  //your code here
  //it means that the amount doesnot contains '.'

}
eles{
  //your code here
  //it contains a '.'
  }
Jayababu
  • 1,641
  • 1
  • 14
  • 30