防止计算字段中出现多个小数点 [英] Prevent multiple decimals points in calculation field

查看:92
本文介绍了防止计算字段中出现多个小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript计算器,它使用以下代码处理计算输入字段中的小数点(也在下面):

I have a JavaScript calculator which uses the following code to handle decimal points in the calculation input field (also below):

$('#button-dot').click(function() {
var lastChar = $('#disp').val().slice(-1);
var firstChar = $('#disp').val().slice(0);
if ($('#disp').hasClass("result")) {
      $('#disp').removeClass("result").val("");
      addChar(this.form.display,'0' + '.');
    }
else if (lastChar == '.'){
    // DO NOTHING
    }
else if (lastChar == '1' || lastChar == '2' || lastChar == '3' || lastChar == '4' || lastChar == '5' || lastChar == '6' || lastChar == '7' || lastChar == '8' || lastChar == '9' || lastChar == '0' && firstChar != '0'){
    addChar(this.form.display,'.');
    }
else if (firstChar == '0'){
    addChar(this.form.display,'0' + '.');
    }
else {
  addChar(this.form.display,'0' + '.');
  }
  $('#disp').removeClass("result");
});

其中 addChar 函数由下式给出:

where the addChar function is given by:

function addChar(input, character) {
  if (input.value == null || input.value == "0" ) {
    input.value = character
    }
  else {
    input.value += character
    }
};

和输入字段:

<INPUT NAME="display" ID="disp" VALUE="0" SIZE="28" MAXLENGTH="25"/>

我希望以限制用户输入多个小数的方式增强我的代码数字(如下所示),同时仍允许计算字符串中的多个小数(也在下面):

I want to enhance my code in such a way as to restrict users from entering multiple decimals in the same number (as seen below), while still allowing multiple decimals in the calculation string (also below):

避免这种情况 -

但请允许此操作 -

But allow this -

我已查看 regex ,因为我了解解决方案我的问题可能以某种方式涉及它,但我不确定如何实现它(我的JavaScript技能仍在进行中!)。

I have looked into regex as I understand the solution to my problem may involve it somehow, but I am not sure how to implement it (my JavaScript skills are still a work in progress!).

我还想过将字符串与任何操作数分开( - ,+,/,*,%并检查结果数组的每个元素的小数点,但我认为这可能有点麻烦。

I also thought of splitting the string with any of the operands (-,+,/,*,%) and checking each element of the resulting array for a decimal point, but I am thinking that might be a tad messy.

任何想法?

推荐答案

所以我添加了一个计数器:

So I added a counter:

dotCount = 0;

并为按钮点做了这个函数和操作数:

And did this for the button-dot function and the operands:

$('#button-dot').click(function() {
var lastChar = $('#disp').val().slice(-1);
var firstChar = $('#disp').val().slice(0);

if (dotCount == 0){
    if ($('#disp').hasClass("result")) {
      $('#disp').removeClass("result").val("");
      addChar(this.form.display,'0' + '.');
    }
    else if (lastChar == '.'){
        // DO NOTHING
    }
    else if (lastChar == '1' || lastChar == '2' || lastChar == '3' || lastChar == '4' || lastChar == '5' || lastChar == '6' || lastChar == '7' || lastChar == '8' || lastChar == '9' || lastChar == '0' && firstChar != '0'){
        addChar(this.form.display,'.');
    }
    else if (firstChar == '0'){
        addChar(this.form.display,'0' + '.');
    }
    else {
        addChar(this.form.display,'0' + '.');
    }
}// END OF dotCount == 0

else if (dotCount == 1){
    //DO NOTHING
}
  $('#disp').removeClass("result");
  dotCount = 1;
});

(以 + 为例):

$('#button-plus').click(function() {
var lastChar = $('#disp').val().slice(-1);
var firstChar = $('#disp').val().slice(0);
if (lastChar == '*' || lastChar == '-' || lastChar == '+' || lastChar == '/' || lastChar == '.' || lastChar == '(' || lastChar == '%'){
    // DO NOTHING
    }
else if (firstChar == '0'){
    // DO NOTHING
    }
else {
  addChar(this.form.display, '+');
  }
  $('#disp').removeClass("result");
  dotCount = 0;
});

像魅力一样工作!谢谢,Chris Christensen的反制想法。

Works like a charm! Thanks, Kai Christensen for the counter idea.

这篇关于防止计算字段中出现多个小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆