验证码登录功能的JSP文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/believe__sss/article/details/79926089

简单的验证码登录功能的实现

<html>
	<head>
		<style>
			 #code{  
                font-family:Arial;  
                font-style:italic;  
                font-weight:bold;  
                border:0;  
                letter-spacing:2px;  
                color:blue;  
            }
		</style>
		<script>
			
	//生成验证码的JS
    var code;
    function createCode(){
       //生成验证码
        code = '';
        //设置验证码的长度为4
        var codeLength = 4;
        var codeV = document.getElementById('code');
        //随机函数生成验证码
        var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z');
       
        for(var i = 0; i < codeLength; i++){
            //取得随机数的索引(0-35)
             var index = Math.floor(Math.random()*36);
             //根据索引取得随机数加到code上
             code += random[index]; 
        }
        //把code值赋给验证码
        codeV.value = code;
    }

    //校验验证码
    function validate(){
        var oValue = document.getElementById('input').value.toUpperCase();
        if(oValue ==0){
            alert('请输入验证码');
        }else if(oValue != code){
            alert('验证码输入有误');
            document.getElementByID('input').value="";//清空文本框
            oValue = ' ';
            //重新生成验证码
            createCode();
        }else{
  
			alert("验证码正确");2018/4/4
        }
    }

    //加载页面的同生成验证码并显示
    window.onload = function (){

        createCode();
    }
		</script>
	</head>
	<body>
		 <div>  
			<input type = "text" id = "input" value="" />  
			<input type = "button" id="code" onclick="createCode()"/>  
			<input type = "button" value = "验证" onclick = "validate()"/>  
		</div> 
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/believe__sss/article/details/79926089