js依存mui.css生成ピクチャ検証コード
56104 ワード
js依存mui.css生成ピクチャ検証コード
関連cssとjs導入経路
https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/css/mui.css
https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/css/mui.min.css
https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/fonts/mui.ttf
https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/js/mui.js
https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/js/mui.min.js
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>js mui.css title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/css/mui.min.css" />
<style>
#verifital_input {
border:1px solid black;
}
/* */
.verificationCode{
width: 4rem;
height: 100%;
position: absolute;
right: 0;
}
canvas{
width: 100%;
height: 100%;
}
#code_img {
width: 100%;
height: 100%;
cursor: pointer;
vertical-align: top;
}
style>
head>
<body>
<div class="mui-content">
<div class="mui-input-row login_input" style="width:300px;">
<label> label>
<input type="text" id="verifital_input" class="mui-input" placeholder=" ">
<div id="verificationCode" class="verificationCode">
<canvas width="100" height="40" id="verifyCanvas">canvas>
<img id="code_img">
div>
div>
div>
body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mui/3.7.1/js/mui.min.js" >script>
<script src="https://lib.baomitu.com/jquery/3.4.1/jquery.js">script>
<script>
var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
'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'];
//
var str = '';
//
var verVal = drawCode();
//
function drawCode () {
var canvas = document.getElementById("verifyCanvas"); // HTML
var context = canvas.getContext("2d"); // 2D
context.fillStyle = "cornflowerblue"; //
context.fillRect(0, 0, canvas.width, canvas.height); //
context.fillStyle = "white"; //
context.font = "25px Arial"; //
var rand = new Array();
var x = new Array();
var y = new Array();
//
for (var i = 0; i < 4; i++) {
rand.push(rand[i]);
rand[i] = nums[Math.floor(Math.random() * nums.length)]
x[i] = i * 20 + 10;
y[i] = Math.random() * 20 + 20;
context.fillText(rand[i], x[i], y[i]);
}
// str
str = rand.join('').toUpperCase();
// 3
for (var i = 0; i < 3; i++) {
drawline(canvas, context);
}
// 30
for (var i = 0; i < 30; i++) {
drawDot(canvas, context);
}
//
convertCanvasToImage(canvas);
}
//
function drawline (canvas, context) {
context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); // x x 0 ,y
context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); // x ,y
context.lineWidth = 0.5; //
context.strokeStyle = 'rgba(50,50,50,0.3)'; //
context.stroke(); // ,
}
// ( 1px , )
function drawDot (canvas, context) {
var px = Math.floor(Math.random() * canvas.width);
var py = Math.floor(Math.random() * canvas.height);
context.moveTo(px, py);
context.lineTo(px + 1, py + 1);
context.lineWidth = 0.2;
context.stroke();
}
//
function convertCanvasToImage (canvas) {
document.getElementById("verifyCanvas").style.display = "none";
var image = document.getElementById("code_img");
image.src = canvas.toDataURL("image/png");
return image;
}
//
document.getElementById('code_img').onclick = function() {
resetCode();
}
//
function resetCode () {
$('#verifyCanvas').remove();
$('#code_img').before('')
//
verVal = drawCode();
}
document.getElementById("verifital_input").addEventListener("change",validate);
//
function validate(){
var inputCode = document.getElementById("verifital_input").value.toUpperCase(); //
if(inputCode.length <= 0) { // 0
alert(" !"); //
$("#verifital_input").focus();
} else if(inputCode != str ) { //
alert(" !@_@"); //
resetCode();//
$("#verifital_input").val("");//
$("#verifital_input").focus();//
} else { //
$("#verifyCanvas").blur();//
alert(" !");
}
};
script>
html>