简体   繁体   English

jQuery或Javascript检查输入值是否包含

[英]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. 我有一个金额字段,但是当用户从字段中跳出时,如果他们没有添加小数,我想在从字段中跳出时添加“ .00”。

Thing is i have no idea on how to do a check if it contains a '.'. 我不知道如何检查它是否包含“。”。 I know how to add the '.00' 我知道如何添加“ .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)
        {

        }
    }
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM