JavaScript生成随机字符

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
<!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>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java计算文件MD5值代码

下一篇:java实现截屏功能的代码