天天看點

驗證碼登入功能的JSP檔案

簡單的驗證碼登入功能的實作

<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" οnclick="createCode()"/>  
			<input type = "button" value = "驗證" onclick = "validate()"/>  
		</div> 
	</body>
</html>