检查整数JavaScript是否形成

时间:2016-03-10 15:49:13

标签: javascript html css

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

编辑:所以我试图解决的问题是如何在theForm.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>

1 个答案:

答案 0 :(得分:0)

您不想创建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.";
}