我如何编写一个函数来检查输入是否实际上是一个整数? (JavaScript)的 [英] How can I write a function that checks to see if an input is actually an integer? (Javascript)

查看:101
本文介绍了我如何编写一个函数来检查输入是否实际上是一个整数? (JavaScript)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我问了一个问题:为什么我的警报总是显示错误的信息? (另外)',其中我很感激有人问我的问题。但是,现在,我还有一个关于我的计算器Javascript程序的问题。



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(您的结果是+结果);


$ / code $ / pre
$ b

HTML:

 < div id =wrapper> 
< div id =form-move>
< div id =numberInput>
< input type =numbername =inputNumber1id =number1placeholder =输入或选择您的第一个号码/>
< input type =numbername =inputNumber2id =number2placeholder =输入或选择您的第二个号码/>
< / div>
< div id =radio-input>
< span class =title>运算符:< / span>
< input type =radioname =radioInputAddid =radioAddvalue =Addition/>
< label for =radioAdd>加法< / label>
< input type =radioname =radioInputAddid =radioDivisionvalue =Division/>
< label for =radioDivision>分部< / label>
< input type =radioname =radioInputAddid =radioMultiplyvalue =Multiplication/>
< label for =radioMultiply>乘以< / label>
< input type =radioname =radioInputAddid =radioSubtractvalue =Subtraction/>
< label for =radioSubtract> Subtract< / label>
< / div>
< div class =submit>
< input type =submitvalue =Enterid =submitonclick =activeButton()/>
< / div>
< / form>
< / div>
< / div>






输入类型名为'inputNumber1'和'inputNumber2',我想编写一个函数,允许Javascript检查并查看用户输入的是否是整数。如果不是,则用户将被给予警报框,并且将被告知他们的输入不是整数,并且他/她将不得不重新提交整数到计算器中。如果用户给出了一个整数,它将继续并检查并查看单击了哪个单选按钮(如上面的函数中所示)。有人能帮助我吗?我一直坚持寻找解决方案的年龄!

解决方案

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

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

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

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



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



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



如果 TRUE 然后你正在处理一个可以由Javascript处理的整数,所以你可以安全地将它强制转换为数字( + userInputString Number(userInputString ),甚至可以使用 parseInt parseFloat 或者数学 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.

这篇关于我如何编写一个函数来检查输入是否实际上是一个整数? (JavaScript)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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