且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何编写一个函数来检查输入是否实际上是一个整数? (JavaScript)的

更新时间:2023-11-23 22:08:28

ECMA 6有一个针对 Number.isInteger ,它在FireFox 16及更高版本中受支持。否则它可以按照MDN页面中的填充方式填充

  if(!Number.isInteger){
Number.isInteger = function isInteger(nVal){
return typeof nVal ===number&& isFinite(nVal)&& nVal> -9007199254740992&& nVal };
}

尽管这不是解决方法,但它是帮助您决定的重要信息关于输入限制以及如何验证。

-9007199254740991和-9007199254740991是Javascript可以处理的最小和最大安全整数。这些限制是16位有效数字,通常将输入限制为15位有效数字,以使您的支票RegExp变得容易。用户输入来自 DOM 中的字符串。



因此:(/ ^ [\ - +]?\d {1,15)$ /)。test(userInputString)



' - '或'+'(或两者皆不),后面跟1到15位数字)



如果 TRUE

然后你正在处理一个可以由Javascript处理的整数,所以你可以安全地将它强制转换为数字( + userInputString Number(userInputString ),甚至可以使用 parseInt parseFloat 或者数学$ c> function)

其他任何东西都变得更加复杂,甚至可能需要使用任意算术库,如 bignumber.js ,除非您没有使用客户端上的号码并将其作为字符串发送给服务器来执行一些工作。


I recently asked a question titled 'Why is my alert always showing the wrong information? (addition)', in which I was grateful that someone asked my question. However, now, I have another question concerning my calculator Javascript program.

JS:

function activeButton() 
{
    if (document.getElementById("radioAdd").checked === true) 
    {
        var valueOne = Number(document.getElementById("number1".value));
        var valueTwo = Number(document.getElementById("number2".value));
        var result = valueOne + valueTwo;
        alert("Your Result Is " + result);
    }       
}

HTML:

<div id="wrapper">
    <div id="form-move">
        <form name = "calculator" id="calculator">
            <div id="numberInput">
                <input type="number" name="inputNumber1" id="number1" placeholder="Type or Select Your First Number" />
                <input type="number" name="inputNumber2" id="number2" placeholder="Type or Select Your Second Number" />
            </div>
            <div id="radio-input">
                <span class="title">Operators:</span>
                <input type="radio" name="radioInputAdd" id="radioAdd" value="Addition" />
                <label for="radioAdd">Addition</label>
                <input type="radio" name="radioInputAdd" id="radioDivision" value="Division" />
                <label for="radioDivision">Division</label>
                <input type="radio" name="radioInputAdd" id="radioMultiply" value="Multiplication" />
                <label for="radioMultiply">Multiply</label>
                <input type="radio" name="radioInputAdd" id="radioSubtract" value="Subtraction" />
                <label for="radioSubtract">Subtract</label>
            </div>
            <div class="submit">
                <input type="submit" value="Enter" id="submit" onclick="activeButton()" />
            </div>
        </form>
    </div>
</div>

For the input types named 'inputNumber1' and 'inputNumber2', I want to write a function that allows Javascript to check and see if the input that the user has given is an integer or not. If it is not, the user will be given an alert box and will be told that their input is not an integer and he/she will have to resubmit integers instead into the calculator. If the user has given an integer, it will continue and check and see which radio button has been clicked (as shown in the function above.) Can someone help me how? I've been stuck for ages finding a solution!

ECMA 6 has a proposal for Number.isInteger, which is supported in FireFox 16 and above. Otherwise it can be polyfilled as given on the MDN page

if (!Number.isInteger) {
  Number.isInteger = function isInteger (nVal) {
    return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
  };
}

This is not the solution though, but it is important information to help you decide about input limits and how to go about validating.

-9007199254740991 and -9007199254740991 are the minimum and maximum safe integers that Javascript can handle. These limits are 16 significant figures, it is usual to limit the input to 15 significant figures to make your check, a RegExp, easy. The user input comes as a string from the DOM.

So: (/^[\-+]?\d{1, 15)$/).test(userInputString)

(can start with a '-' or a '+' (or neither) followed by 1 to 15 digits)

If that is TRUE then you are working with an integer that can be handled by Javascript and so you can safely coerce it to a number (+userInputString or Number(userInputString), or even use parseInt or parseFloat or a Math function)

Anything else and things become more complicated, and could even require the use of an Arbitrary Arithmatic Library like bignumber.js unless you are not working with the number on the client and are sending it as a string to a server to do some work with.