日期:2014-05-16  浏览次数:20537 次

新手求一个随机密码生成的js函数
本人不会javascript,求一个生成随机密码的函数。
要求是这样的:

密码长度要求六位,随机产生,后两位=第1位和第3位之和,如果之和小于10则倒数第2位补0
比如:
123404 后两位04=第一位的“1”+第三位的“3”
932311
820208
939418
能明白吗 比如上面这个939418 后两位18=第一位9+第3位9

function setpwd(){
...
return mima
}

谢谢啊


------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
    </head>
    <body>
        <script>
            function setpwd(){
                var mima = [];
                for(var i = 0; i < 4; i++){
                    mima.push( Math.floor(Math.random() * 10) );
                }
                var tmp = mima[0] + mima[2];
                if(tmp < 10){
                    tmp = '0' + tmp;
                }
                mima.push(tmp);
                mima = mima.join('');
                return mima;
            }
            var test = setpwd();
            alert( test )
        </script>
    </body>
</html>

------解决方案--------------------
var rd = Math.random();
var mima1 = Math.floor((rd*10));
var mima2 = Math.floor((rd*100)) - mima1*10;
var mima3 = Math.floor((rd*1000)) - mima1*100 - mima2*10;
var mima4 = Math.floor((rd*10000)) - mima1*1000 - mima2*100 - mima3*10;

var mima56=mima1 + mima3;
if (( mima56<10))
{
mima56 = '0' + (mima56);
}
var mima = ''+mima1+mima2+mima3+mima4+mima56;
------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
    </head>
    <body>
        <a href="#" id="test">密码</a>
        <script>
            function setpwd(){
                var mima = [];
                for(var i = 0; i < 4; i++){
                    mima.push( Math.floor(Math.random() * 10) );
                }
                var tmp = mima[0] + mima[2];
                if(tmp < 10){
                    tmp = '0' + tmp;
                }
                mima.push(tmp);
                mima = mima.join('');
                return mima;
            }
            var test = setpwd();
            document.getElementById('test').href = 'test.jsp?pwd='+test;
        </script>
    </body>
</html>

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
    </head>
    <body>
        <script>
            function setpwd(){
                var mima = [];
                for(var i = 0; i < 4; i++){
                    mima.push( Math.floor(Math.random() * 10) );
                }
                var tmp = mima[0] + mima[2];
                if(tmp < 10){
                    tmp = '0' + tmp;
                }
                mima.push(tmp);
                mima = mima.join('');
                return mima;
            }
            document.write('<a href="http://www.xxx.jsp?pwd='+setpwd()+'" >click</a>')
        </script>
    </body>
</html>