murday1983
murday1983

Reputation: 4026

JQuery Or Javascript To Check If Input Value Contains

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)
        {

        }
    }
}

Upvotes: 0

Views: 791

Answers (2)

Barmar
Barmar

Reputation: 781741

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 '';
    }
});

Upvotes: 1

Jayababu
Jayababu

Reputation: 1621

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 '.'
  }

Upvotes: 0

Related Questions