JS實現(xiàn)隨機生成驗證碼
本文實例為大家分享了JS實現(xiàn)隨機生成驗證碼的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> </style> </head> <body> <div> <h1>111111</h1><a href="#" rel="external nofollow" >看不清換一張</a> </div> 驗證碼:<input type="text" value="" /> <button>確定</button> </body> <script> var arr = ["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", "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", 0,1,2,3,4,5,6,7,8,9]; var h1=document.querySelector("h1"); var btn=document.querySelector("button"); var change=document.querySelector("a"); var input=document.querySelector("input"); function fn(){ var arr1=Math.floor(Math.random()*62); var arr2=Math.floor(Math.random()*62); var arr3=Math.floor(Math.random()*62); var arr4=Math.floor(Math.random()*62); var arr5=Math.floor(Math.random()*62); var arr6=Math.floor(Math.random()*62); var yz=""+arr[arr1]+arr[arr2]+arr[arr3]+arr[arr4]+arr[arr5]+arr[arr6]; h1.innerHTML=yz; btn.onclick=function(){ alert(input.value); if(input.value==yz){ alert("正確"); }else{ alert("錯誤"); } } } fn(); change.addEventListener("click",fn); </script> </html>
1、Math.floor() 方法
Math.floor(x)
floor() 方法返回小于等于x的最大整數(shù)。
如果傳遞的參數(shù)是一個整數(shù),該值不變。
2、Math.random()方法
random() 方法可返回介于 0(包含) ~ 1(不包含) 之間的一個隨機數(shù)。
使用以上兩種方法可以實現(xiàn)取隨機數(shù)的功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js學(xué)習(xí)總結(jié)_選項卡封裝(實例講解)
下面小編就為大家?guī)硪黄猨s學(xué)習(xí)總結(jié)_選項卡封裝(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07用js實現(xiàn)的檢測瀏覽器和系統(tǒng)的函數(shù)
檢測各種瀏覽器、系統(tǒng)的JS代碼2009-04-04

JavaScript里四舍五入函數(shù)round用法實例