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

时间:2016-09-08 14:57:56

标签: javascript jquery

我有一个金额字段但是当用户标出该字段时,如果他们没有添加小数,我想在他们选中字段时添加“.00”。

我不知道如何检查它是否包含'。'。我知道如何添加'.00'

这是我目前的代码

<label> <index1>:<value1> <index2>:<value2> ...
.
.
.

2 个答案:

答案 0 :(得分:1)

不应特别检查它是否有小数,而应将其转换为您想要的数字格式。

$("#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 '';
    }
});

答案 1 :(得分:0)

你可以这样做。

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