【问题标题】:How to restrict two digit numbers in the captcha code?如何限制验证码中的两位数字?
【发布时间】:2014-05-14 21:26:51
【问题描述】:

在上面的验证码中,我得到了两位数字,即; 0 到 10。但我希望我的验证码介于 0-9 之间。我怎样才能在我的验证码中限制数字 10?有没有脚本可以停止两位数?任何帮助,将不胜感激!!

<html>
    <head>
    <title>Captcha</title>

        <script type="text/javascript">

       //Created / Generates the captcha function    
        function DrawCaptcha()
        {
            var a = Math.ceil(Math.random() * 10)+ '';
            var b = Math.ceil(Math.random() * 10)+ '';       
            var c = Math.ceil(Math.random() * 10)+ '';  
            var d = Math.ceil(Math.random() * 10)+ '';  
            var e = Math.ceil(Math.random() * 10)+ '';  
            var f = Math.ceil(Math.random() * 10)+ '';  
            var g = Math.ceil(Math.random() * 10)+ '';  
            var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
            document.getElementById("txtCaptcha").value = code
        }

        // Validate the Entered input aganist the generated security code function   
        function ValidCaptcha(){
            var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
            var str2 = removeSpaces(document.getElementById('txtInput').value);
            if (str1 == str2) return true;        
            return false;

        }

        // Remove the spaces from the entered and generated code
        function removeSpaces(string)
        {
            return string.split(' ').join('');
        }


        </script>



    </head>
    <body onload="DrawCaptcha();">
    <table>
    <tr>
        <td>
            Welcome To Captcha<br />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" id="txtCaptcha" 
                style="background-image:url(1.jpg); text-align:center; border:none;
                font-weight:bold; font-family:Modern" />
            <input type="button" id="btnrefresh" value="Refresh" onclick="DrawCaptcha();" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" id="txtInput"/>    
        </td>
    </tr>
    <tr>
        <td>
            <input id="Button1" type="button" value="Check" onclick="alert(ValidCaptcha());"/>
        </td>
    </tr>
    </table>
    </body>
    </html> 

【问题讨论】:

    标签: javascript jquery security captcha recaptcha


    【解决方案1】:

    在处理随机数时使用Math.floor()

    Math.rand() 返回一个介于 0 和 1 之间的数字,但包含 0 而不包含 1。

    如果乘以 10,则包括零,但不包括 10。

    这意味着如果您将 向上舍入,您将永远*不会*得到0

    所以,把它四舍五入;)

    *为了得到零,随机选择的数字本身必须完全为零。从数学上讲,在实数范围内选择任何精确实数的几率为零,但在实践中,由于数字在计算中的限制,它的可能性要大得多,但仍然不太可能。

    【讨论】:

      【解决方案2】:

      您也许应该使用Math.floor() 而不是Math.ceil()

      Math.random() * 10 为您提供 [0;10[ 之间的数字(表示包括 0,不包括 10)。所以当你的结果数字应该在 0 到 9 之间时,你需要向下而不是向上取整。

      见:

      【讨论】:

      • 尽管如此,w3schools.com 在 w3c 一致性方面仍有争议,它对初学者来说是一个快速的帮助。如果您不喜欢它,我建议您提供替代网站 :-)
      猜你喜欢
      • 2016-05-21
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2016-12-11
      • 2018-09-22
      相关资源
      最近更新 更多