JS如何生成隨機驗證碼
本文實例為大家分享了JS生成隨機驗證碼的具體代碼,供大家參考,具體內(nèi)容如下

在網(wǎng)站中我們很常見到形形色色的驗證碼,今天我們來用JS來生成一個隨機的二維碼。
我們需要用到canvas來進行驗證碼的繪制
什么是Canvas
HTML5 的 canvas 元素使用 JavaScript 在網(wǎng)頁上繪制圖像。
畫布是一個矩形區(qū)域,您可以控制其每一像素。
canvas 擁有多種繪制路徑、矩形、圓形、字符以及添加圖像的方法。
思路
我們要做的二維碼首先要有隨機的數(shù)字,其次就是要有隨機的位置。
HTML
<canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"> </canvas>
JS
function getVerification() { //二維碼
var ctx = document.getElementById("canvas").getContext("2d");
// 清空畫布
ctx.clearRect(0,0, 400, 400);
// 設(shè)置字體
ctx.font = "128px bold 黑體";
// 設(shè)置垂直對齊方式
ctx.textBaseline = "top";
// 設(shè)置顏色
ctx.fillStyle = randomColor();
// 繪制文字(參數(shù):要寫的字,x坐標(biāo),y坐標(biāo))
ctx.fillText(getRandomNum(10), 0, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 50, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 100, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 150, getRandomNum(50));
}
我們使用ctx.fillStyle = randomColor();來設(shè)置隨機的顏色,每寫一個數(shù)字換一個顏色,randomColoe()函數(shù)代碼如下,可以隨機生成十六進制顏色碼。
function randomColor() {
var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
var colorArray = colorValue.split(",");
var color = "#";
for (var i = 0; i < 6; i++) {
color += colorArray[Math.floor(Math.random() * 16)];
}
return color;
}
我們使用getRandomNum()來獲取隨機顯示的數(shù)字和隨機每次字體的y軸方向的位置。驗證碼的每個數(shù)字分別進行獲取。傳入的參數(shù)n來確定隨機數(shù)范圍。代碼如下:
function getRandomNum(n){
return parseInt(Math.random() * n);
}
完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2</title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"></canvas>
<span id="yanzhengma"></span><button onclick="getVerification()">看不清</button>
<script>
function randomColor() {
var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
var colorArray = colorValue.split(",");
var color = "#";
for (var i = 0; i < 6; i++) {
color += colorArray[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomNum(n){
return parseInt(Math.random() * n);
}
function getVerification() {
var ctx = document.getElementById("canvas").getContext("2d");
ctx.clearRect(0,0, 400, 400);
// 設(shè)置字體
ctx.font = "128px bold 黑體";
// 設(shè)置垂直對齊方式
ctx.textBaseline = "top";
// 設(shè)置顏色
ctx.fillStyle = randomColor();
// 繪制文字(參數(shù):要寫的字,x坐標(biāo),y坐標(biāo))
ctx.fillText(getRandomNum(10), 0, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 50, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 100, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 150, getRandomNum(50));
}
getVerification();
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IE與Firefox在JavaScript上的7個不同寫法小結(jié)
盡管那需要用長串的、沉悶的不同分支代碼來應(yīng)付不同瀏覽器的日子已經(jīng)過去,偶爾還是有必要做一些簡單的區(qū)分和目標(biāo)檢測來確保某塊代碼能在用戶的機器上正常運行。2009-09-09
基于Bootstrap模態(tài)對話框只加載一次 remote 數(shù)據(jù)的解決方法
下面小編就為大家?guī)硪黄贐ootstrap模態(tài)對話框只加載一次 remote 數(shù)據(jù)的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
JS實現(xiàn)禁止用戶使用Ctrl+鼠標(biāo)滾輪縮放網(wǎng)頁的方法
這篇文章主要介紹了JS實現(xiàn)禁止用戶使用Ctrl+鼠標(biāo)滾輪縮放網(wǎng)頁的方法,涉及javascript頁面元素與事件相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
淺談webpack devtool里的7種SourceMap模式
這篇文章主要介紹了淺談webpack devtool里的7種SourceMap模式,主要介紹了這7種模式的使用和打包編譯后的結(jié)果的不同,非常具有實用價值,有興趣的可以了解一下2019-01-01
Kettle中使用JavaScrip調(diào)用jar包對文件內(nèi)容進行MD5加密的操作方法
這篇文章主要介紹了Kettle中使用JavaScrip調(diào)用jar包對文件內(nèi)容進行MD5加密的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
用Javascript評估用戶輸入密碼的強度(Knockout版)
早上看到博友6點多發(fā)的一篇關(guān)于密碼強度的文章(連接),甚是感動(周末大早上還來發(fā)文)2011-11-11
Javascript中獲取出錯代碼所在文件及行數(shù)的代碼
之前在做一個Javascript的日志控制臺功能模塊,希望能夠在Javascript代碼出錯時捕獲此錯誤,并將出錯的文件及相應(yīng)的行數(shù)打印到控制臺并匯報給服務(wù)器。2010-09-09

