JavaScript 纯验证码编写

JavaScript编写简洁验证码

HTML部分

<HTML>
	<head>
		<meta charset="UTF-8">
		<title><title>
		<style type="text/css">
			.code{
    
    
				font-family:Arial;
				font-weight: bold;
				border: 0;
				letter-spacing: 3px;
				color: blue;
			}
		</style>
		<script type="txet/JavaScript" src="附件.js"></script>
	</head>
	<body>
		<div>
			<input type="text" id="input" />
			<input type="button" id="checkCode" class="code" οnclick="createCode()" />
			<input type="button" value="验证" οnclick="validate()" />
		</div>
	</body>

JavaScript部分

var code;
window.onload = functiion(){
    
    
	createCode();
	}
function createCode(){
    
    
	codr = "";
	var codeLength = 6;//验证码长度
	var checkCode = document.getElementById('checkCode');
	var strArr = [0,1,2,3,4,5,6,7,8,9];
	for (var j = 0; j < 26; j++){
    
    
		strArr.push(String.fromCharCode(65+j));
   	 }   
   	for(var i = 0; i < 6; i++) {
    
    //循环
   		var charIndex = Math.floor(Math.random() * strArr.length); 
   		//取得随机数的索引   
 		code += strArr[charIndex];
	}
	 checkCode.value = code;
function validate() {
    
    
	var inputCode = document.getElementById("input").value.toUpperCase(); 
 	//取得输入的验证码并转化为大写
	if(inputCode.length <= 0) {
    
     //长度判断   
		alert("请输入验证码!");
	} 
	else if(inputCode != code) {
    
     //输入正误判断 
		alert("验证码输入错误!");   
		createCode(); //刷新验证码   
 	} 
 	else {
    
      
		alert("正确"); 
  	}
  }

猜你喜欢

转载自blog.csdn.net/weixin_44332119/article/details/105552087