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>

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10048.html

(0)
上一篇 2021年7月19日 09:58
下一篇 2021年7月19日 09:58

相关推荐

发表回复

登录后才能评论