用js在前台及后台生成随机字符串

原创 2016年06月02日 12:51:24

用js在前台生成随机字符串

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>随机密码生成器 - 随机字符串生成器 - Random Password Generator | C7TOOL</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" />
<meta name="description" content="随机密码生成器 - 随机字符串生成器 - Random Password Generator" />


<link rel="stylesheet" href="http://cdn.bootcss.com/pure/0.4.2/pure-min.css">
<link rel="stylesheet" href="../style.css">


<style>
#header {
    max-width: 800px;
margin-left:130px;
}
#main {
    max-width: 500px;
margin-left:130px;
}


ul {
    margin: 0;
    padding: 0;
    border-top: 1px solid #eee;
    list-style: none;
    font-size: 14px;
}


li {
    clear: both;
    overflow: hidden;
    padding: .5em 0;
    border-bottom: 1px solid #eee;
}


.pure-form label {
    display: block;
    overflow: hidden;
    margin: 0;
}


.pure-form span {
    float: left;
    margin: 6px 0;
}


input {
    float: right;
    margin-top: 9px;
}


#password_length,#password_quantity {
    margin-top: 0;
    width: 5em;
}


#generate {
    margin: 1em 0;
}


#output {
font-family: Monaco, Menlo, Consolas, Inconsolata, 'Deja Vu Sans Mono', 'Droid Sans Mono', 'Lucida Console', 'Courier New', Courier, monospace;
}
</style>
</head>
<body> 
<div id="header">
    <h1>随机密码生成器 - 随机字符串生成器</h1>
    <h2>Random Password Generator</h2>
</div>


<div id="main" class="pure-form">
<ul>
<li><label><input type="checkbox" id="include_number" checked /><span>数字</span></label></li>
<li><label><input type="checkbox" id="include_lowercaseletters" checked /><span>小写字母</span></label></li>
<li><label><input type="checkbox" id="include_uppercaseletters" checked /><span>大写字母</span></label></li>
<li><label><input type="checkbox" id="include_punctuation" /><span>标点符号</span></label></li>
<li><label><input type="checkbox" id="password_unique" /><span>字符不重复</span></label></li>
<li><label><span>密码长度</span><input type="number" id="password_length" min="0" value="8" /></label></li>
<li><label><span>密码数量</span><input type="number" id="password_quantity" min="0" value="1" /></label></li>
</ul>
<button id="generate" class="pure-button pure-button-primary pure-input-1">生成密码</button>
<textarea id="output" class="pure-input-1" cols="40" rows="7" readonly></textarea>
</div>


<script>
var $ = function(s){return document.getElementById(s)},
$includeNumber = $("include_number"),
$includeLowercaseletters = $("include_lowercaseletters"),
$includeUppercaseletters = $("include_uppercaseletters"),
$includePunctuation = $("include_punctuation"),
$passwordUnique = $("password_unique"),
$passwordLength = $("password_length"),
$passwordQuantity = $("password_quantity"),
$generate = $("generate"),
$output = $("output");


function rand(max) {
    return Math.floor(Math.random() * max);
};


$generate.onclick = function () {
    var chars = "";


    if ($includeNumber.checked) chars += "0123456789";
    if ($includeLowercaseletters.checked) chars += "abcdefghijklmnopqrstuvwxyz";
    if ($includeUppercaseletters.checked) chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if ($includePunctuation.checked) chars += "`~!@#$%^&*()-_=+[{]}\|;:'\",<.>/?";


    var passwords = [],
passwordUnique = $passwordUnique.checked;
    for (var i = 0, l = $passwordQuantity.value; i < l; i++) {
        var _chars = chars.split(""),
password = "";
        for (var j = 0, k = $passwordLength.value; j < k; j++) {
            if (_chars.length < 1) break;
            var index = rand(_chars.length);
            password += _chars[index];
            if (passwordUnique) _chars.splice(index, 1);
        };
        passwords.push(password);
    };
    $output.value = passwords.join("\n");
};


$output.onfocus = function () {
    this.select();
}
</script>


</body>
</html>

用js在后台生成随机字符串


<%@ LANGUAGE='JAVASCRIPT' %>
<%
function rand(max) {
    return Math.floor(Math.random() * max);
};


function getRand(includeNumber,includeLowercaseletters,includeUppercaseletters,includePunctuation,passwordUnique,passwordQuantity,passwordLength)
{
    var chars = "";


    if (includeNumber) chars += "0123456789";
    if (includeLowercaseletters) chars += "abcdefghijklmnopqrstuvwxyz";
    if (includeUppercaseletters) chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (includePunctuation) chars += "`~!@#$%^&*()-_=+[{]}\|;:'\",<.>/?";


    var passwords = [] ;
    for (var i = 0, l = passwordQuantity; i < l; i++) {
        var _chars = chars.split(""),
password = "";
        for (var j = 0, k = passwordLength; j < k; j++) {
            if (_chars.length < 1) break;
            var index = rand(_chars.length);
            password += _chars[index];
            if (passwordUnique) _chars.splice(index, 1);
        };
        passwords.push(password);
    };
    return passwords.join("<br/>");
}


Response.Write(getRand(1,1,1,1,1,8,12));
%>



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

前台通过JS请求后台的几种方法

前台js请求后台的几种方法的汇总

js 生成随机字符串

今儿碰到这么一个小问题啊,要前端生成一个随机字符串的token,特地写了个小算法,生成随机数字,和随机字符串! //800~1500随机数 var num = Math.random()*700 +...

JS生成随机字符串

function randomString(len) {   len = len || 32;   var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprs...

as3.0 生成随机字符串类

js 数组随机字符串(广告不重复)

今天一个网友想让他的广告随机显示,每次刷新广告的内容都不一样,经过参考源码网站分析就是通过下面代码实现,特分享下方便需要的朋友 比较好的地方就是js数组的操作,不重复的数组id显示,完美实现。 ...

Java生成随机字符串

Java生成随机字符串,必须包含数字、小写字母、大写字母 一道算法题,生成随机字符串,必须包含数字、小写字母、大写字母。 为了生成随机数方便,特别编写StdRandom类(注1),API如...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)