检查整数 JavaScript 是否形成

Checking if integer JavaScript forms

本文关键字:是否 JavaScript 整数 检查      更新时间:2023-09-26

我已经开始学习javascript,我无法获得表单的安全代码部分(并且我尚未修复其他内容,例如卡号)以发出警报,如果他们没有输入3个整数,我可以让它发出警报,如果该人没有输入3个整数/字符串/符号等......但是>或<3。但是,如果他们传递的东西不是整数,我无法让它提醒用户。谢谢!。

编辑:所以我试图解决的问题是如何在 Form.cvs.value 上运行我的 is_int 函数我很抱歉,如果我不清楚它有点混乱。

<script type="text/JavaScript">
    function is_int(value){ 
        if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
            return true;
        } else { 
            return false;
        } 
    };
    function verification(theForm) {
        content = "";
        var cardLen = (theForm.cardLength.value).length;
        var securitycode = new is_int(theForm.cvs.value);
            if (cardLen !== 16) {
                content += "Please make sure you've entered 16 digits.";
            }
            if ((theForm.userName.value).length === 0) {
                content += "Please make sure you've entered the correct name.";
            }
            if ((theForm.month.value) < 1 || theForm.month.value > 12 || theForm.month.value === "" || theForm.month.value === "MM") {
                content += "Please make sure the you've entered the correct month.";
            }
            if ((theForm.year.value) < 2016 || ((theForm.year.value) === "" )) {
                content += "Please make sure you've entered the correct expiry year.";
            }
            if ( !securitycode || ( (theForm.cvs.value).length !== 3) ) {
                content += "Please make sure you've entered the correct security code.";
            }
            if (!content == "") {
                alert (content); return false;
            }
        };
</script>

</head>
<body>
    <br>
    <br>
    <br>
    <center><h1>Checkout:</h1></center>
    <div style="position:absolute; left:600px; top:200px;">
        <form name="myForm" class="theForm" onSubmit="return verification(this)" >
        Card Number: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Expiration:
    <br>
        <input type="text" name="cardLength"> <input type="text" name="month" style="width:30px" value="MM"> - <input type="text" name="year" style="width:30px" value="YY">
    <br>
        Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Security Code:
    <br>
        <input type="text" name="userName"> <input type="text" name="cvs" style="width:30px">
    <br>

    <br>
        <input type="submit" value="Submit">
        </form>
    </div>

您不想创建new is_int。New 创建一个对象的实例并调用其构造函数,但您只需要从函数中获取返回值。

if ( !is_int(theForm.cvs.value) || theForm.cvs.value.length !== 3 ) {
    content += "Please make sure you've entered the correct security code.";
}