Pergunta

In the above captcha code i am getting two digit numbers ie; 0 to 10.but i want my captcha to be between 0-9 .how can i restict number 10 in my captcha code???is there any script to stop the two digit number? Any help would be appreciated!!

<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> 
Foi útil?

Solução

Use Math.floor() when working with random numbers.

Math.rand() returns a number between zero and one, but zero is included while one is not.

If you multiply by 10, then zero is included but 10 is not.

This means that if you round up, you will never* get 0 as a result.

So, round it down ;)

*In order to get zero, the randomly-chosen number must itself be exactly zero. Mathematically, the odds of picking any exact real number in a range of real numbers is zero, however in practice, due to the limitations of numbers in computing, it's much more possible but still ridiculously unlikely.

Outras dicas

You should maybe use Math.floor() instead of Math.ceil()

Math.random() * 10 gets you an number between [0;10[ (means including 0, excluding 10). So you need to round down not up, when your resulting number should be between 0 and 9.

see:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top