How can I write a function that checks to see if an input is actually an integer? (Javascript)

ぃ、小莉子 提交于 2020-01-06 13:29:29

问题


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!


回答1:


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.




回答2:


Use any of the Math to integer function to check for Integer entry.

if ((valueOne === Math.round(valueOne)) &&
    (valueTwo === Math.round(valueTwo))) {
    var result = valueOne + valueTwo;
} else {
    var result = "Please enter Integers.";
};


来源:https://stackoverflow.com/questions/21370606/how-can-i-write-a-function-that-checks-to-see-if-an-input-is-actually-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!