用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - javascript代码库

JS生成随机字符

2012-12-18 作者: 云代码会员举报

[javascript]代码库

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Random Chars</title>
<style type="text/css">
body {
    font-size: 10px;
    text-align: center;
}
 
td {
    border: 1px solid #137d5c;
    text-align: center;
}
 
input {
    border: 1px solid #137d5c;
    font-size: 12px;
    text-align: center;
}
</style>
<script language="JavaScript">
    function getRandomChars(length) {
        if (isNaN(length)) {
            document.getElementById("error").innerText = "闹呢!输入数字!";
            return;
        } else if (parseInt(length) <= 0) {
            document.getElementById("error").innerText = "你会好好的不!!!";
            return;
        } else if (parseInt(length) > 16) {
            document.getElementById("error").innerText = "太长了!最大16!瞎试什么!老实儿的!";
            return;
        } else
            document.getElementById("error").innerText = "";
        var ranChars = "";
        var chars = "";
        var numChars = "0123456789";
        var lowChars = "abcdefghijklmnopqrstuvwxyz";
        var upChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var symChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?"
        if (document.ranForm.numChars.checked)
            chars += numChars;
        if (document.ranForm.lowChars.checked)
            chars += lowChars;
        if (document.ranForm.upChars.checked)
            chars += upChars;
        if (document.ranForm.symChars.checked)
            chars += symChars;
        if (chars.length < 1) {
            document.getElementById("error").innerText = "闹呢!P都不选生成个P啊!";
            return;
        } else
            document.getElementById("error").innerText = "";
        for ( var i = 0; i < length; i++) {
            ranChars = ranChars + chars.charAt(Math.random() * chars.length);
        }
        document.getElementById('ranChars').value = ranChars;
    }
</script>
</head>
<body>
    <center>
        <form name="ranForm">
            <table width="310">
                <tr>
                    <td>数字<input name="numChars" type="checkbox" />小写字母<input
                        name="lowChars" type="checkbox" />大写字母<input name="upChars"
                        type="checkbox" />符号<input name="symChars" type="checkbox" />
                    </td>
                </tr>
                <tr>
                    <td>长度<input id="len" type="text" size="1" maxlength="2"
                        value="数字" /><input id="getRan" type="button" value="生成随机字符"
                        onClick="getRandomChars(document.getElementById('len').value)" />
                        <input id="ranChars" type="text" readonly="readonly" size="22" /><br />
                        <div id="error" style="color: red;"></div>
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...