js實(shí)現(xiàn)隨機(jī)8位驗(yàn)證碼
開發(fā)思路:
1.畫出放置驗(yàn)證碼的模塊、一個寫有“看不清…”的小塊,以及輸入驗(yàn)證碼的文本框
2.獲取各個模塊
3.封裝一個函數(shù)Yan_ma(),設(shè)置驗(yàn)證碼為8位,里面含有數(shù)字,小寫字母,小寫字母和中文。每種類型出現(xiàn)的可能性為25%。
4.隨機(jī)數(shù)字在0-9,之間。對Math.ramand()向下取整。
5.隨機(jī)大小寫字母使用fromCharCode() 方法:將 Unicode 編碼轉(zhuǎn)為一個字符,例如:
var n = String.fromCharCode(65); cosole.log(n); //輸出j結(jié)果為A
大寫字母(65-91) 小寫字母(97-123)
var s = String.fromCharCode(Math.floor(Math.random() * 26 + 65)); var s = String.fromCharCode(Math.floor(Math.random() * 26 + 97));
6.隨機(jī)中文,聲明變量letter放置中文字符串,使用charAt()隨機(jī)在letter中獲得某個漢字。
var letter = "如若可以親愛的請?jiān)S我青燈墨下執(zhí)一筆素箋今生為你吟盡千回百轉(zhuǎn)念"; var s = letter.charAt(Math.floor(Math.random() * letter.length));
7.給每位驗(yàn)證碼設(shè)隨機(jī)的顏色,字體大小,相對文本位置,旋轉(zhuǎn)角度。給顏色封裝一個函數(shù),使用十六進(jìn)制顏色(如:#ffffff)
//隨機(jī)顏色
function fontcolor(){
var s1="";
for(var k=0;k<6;k++){
var z=[0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"];
var m=z[Math.floor(Math.random() * z.length)];
s1 +=m;
}
return "#"+s1;
}
隨機(jī)位置和隨機(jī)旋轉(zhuǎn)角度的方法相同
隨機(jī)位置可能為向上下左右偏移 8px, 隨機(jī)旋轉(zhuǎn)角度可能為繞著z軸旋轉(zhuǎn)(±45度)。
8.提前聲明一個空字符串 str 讓每位驗(yàn)證碼用字符串連接起來.
var s = String.fromCharCode(Math.floor(Math.random() * 26 + 97));
str+="<span style ='transform:rotatez("+ zhuan +"deg);left:"+topset+"px; color: " +color +" ;font-size:"+size+"px ;top:"+topset+"px'>"+s+"</span>";
arr+=s;
9.讓8位驗(yàn)證碼出現(xiàn)在第一個大模塊中的innerHTML中。
10.給寫有“看不清”的span標(biāo)簽添加點(diǎn)擊事件,點(diǎn)擊時,調(diào)用函數(shù)Yan_ma,刷新驗(yàn)證碼。
11.如果輸入的驗(yàn)證碼不正確,則彈出“驗(yàn)證成功”,否則彈出“驗(yàn)證失敗”。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>隨機(jī)驗(yàn)證碼</title>
<script>
var arr="";
window.onload=function() {
var maa = document.getElementsByClassName("block")[0];
var looking = document.getElementById("look");
var put = document.getElementById("text");
var btnn = document.getElementById("btn");
Yan_ma(maa);
looking.onclick=function (){
Yan_ma(maa);
};
btnn.onclick=function(){
if(put.value.toLowerCase()==arr.toLowerCase()){
alert ("驗(yàn)證成功");
}
else{
alert ("驗(yàn)證失敗");
Yan_ma(maa);
}
}
};
function Yan_ma(aim) {
arr ="";
var str="";
for (var i = 0; i < 8; i++) {
//隨機(jī)數(shù) Math.random 0-1 的隨機(jī)值
var n = Math.random();
//隨機(jī)顏色
var color=fontcolor();
//隨機(jī)大小
var size=Math.floor (Math.random ()*12 +20);
//隨機(jī)位置
var g=Math.random() <0.5 ? -1: 1;
var topset=Math.random ()*g*8;
//隨機(jī)旋轉(zhuǎn)
var h=Math.random() <0.5 ? -1: 1;
var zhuan=Math.random ()*h*45;
if (n < 0.25) {
//隨機(jī)數(shù)字
var s = Math.floor(Math.random() * 10);
str+="<span style='transform:rotatez("+ zhuan +"deg);left:"+topset+"px; color: " +color +";font-size:"+size+"px ;top:"+topset+"px'>"+s+"</span>";
arr+=s;
}
//隨機(jī)大寫字母 //65-91
else if (n >= 0.25 && n < 0.5) {
var s = String.fromCharCode(Math.floor(Math.random() * 26 + 65));
str+="<span style='transform:rotatez("+ zhuan +"deg); left:"+topset+"px; color: " +color +";font-size:"+size+"px ;top:"+topset+"px'>"+s+"</span>";
arr+=s;
}
//隨機(jī)小寫字母 97-123
else if (n >=0.5 && n < 0.75) {
var s = String.fromCharCode(Math.floor(Math.random() * 26 + 97));
str+="<span style ='transform:rotatez("+ zhuan +"deg);left:"+topset+"px; color: " +color +" ;font-size:"+size+"px ;top:"+topset+"px'>"+s+"</span>";
arr+=s;
}
//隨機(jī)文字
else {
var letter = "如若可以親愛的請?jiān)S我青燈墨下執(zhí)一筆素箋今生為你吟盡千回百轉(zhuǎn)念";
var s = letter.charAt(Math.floor(Math.random() * letter.length));
str+="<span style='transform: rotatez("+ zhuan +" deg) ; left:"+topset+"px; color: " +color +";font-size:"+size+"px;top:"+topset+"px '>"+s+"</span>";
arr+=s;
}
}
aim.innerHTML =str;
}
function fontcolor(){
var s1="";
for(var k=0;k<6;k++){
var z=[0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"];
var m=z[Math.floor(Math.random() * z.length)];
s1 +=m;
}
return "#"+s1;
}
</script>
<style>
.block{
width:250px;
height:60px;
background:url("bg2.png") no-repeat center;
background-size: 100%;
text-align: center;
line-height: 60px;
}
.block span{
position: relative;
display: inline-block;
width:20px;
margin:5px 3px;
}
#look{
color: #9200ff;
}
#look:hover{
cursor: pointer;
}
</style>
</head>
<body>
<div class="block">
</div>
<span id="look">看不清...</span>
<br/>
<input type="text" id="text" placeholder="請輸入驗(yàn)證碼" />
<button id="btn">驗(yàn)證</button>
</body>
</html>
出現(xiàn)的驗(yàn)證碼

當(dāng)輸入正確驗(yàn)證碼時

當(dāng)沒輸入錯誤驗(yàn)證碼時

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)隨機(jī)數(shù)字字母驗(yàn)證碼
- js隨機(jī)生成一個驗(yàn)證碼
- JS如何生成隨機(jī)驗(yàn)證碼
- JavaScript 隨機(jī)驗(yàn)證碼的生成實(shí)例代碼
- 用jsp頁面生成隨機(jī)的驗(yàn)證數(shù)字碼示例
- JavaScript實(shí)現(xiàn)隨機(jī)五位數(shù)驗(yàn)證碼
- 基于JS實(shí)現(xiàn)一個隨機(jī)生成驗(yàn)證碼功能
- JS實(shí)現(xiàn)4位隨機(jī)驗(yàn)證碼
- JS 實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能
- Javascript實(shí)現(xiàn)仿QQ隨機(jī)數(shù)驗(yàn)證
相關(guān)文章
原生js實(shí)現(xiàn)鍵盤控制div移動且解決停頓問題
這篇文章主要給大家介紹了如何利用原生js實(shí)現(xiàn)鍵盤控制div移動,并且解決在移動過程中的停頓問題,文中給出了詳細(xì)的示例代碼,相信對大家的理解和學(xué)習(xí)很有幫助,下面跟著小編一起來看看吧。2016-12-12
10行原生JS實(shí)現(xiàn)文字無縫滾動(超簡單)
下面小編就為大家分享一篇10行原生JS實(shí)現(xiàn)文字無縫滾動的效果,特別簡單,具有很好的參考價值,希望對大家有所幫助2018-01-01
JS小功能(offsetLeft實(shí)現(xiàn)圖片滾動效果)實(shí)例代碼
使用flutter創(chuàng)建可移動的stack小部件功能
Bootstrap每天必學(xué)之導(dǎo)航條(二)
Javascript 是你的高階函數(shù)(高級應(yīng)用)
JS實(shí)現(xiàn)隨機(jī)顏色的3種方法與顏色格式的轉(zhuǎn)化

