【Labs】JavaScriptで簡易CAPTCHA認証 - web design lab
にほんブログ村 デザインブログ Webデザインへ PVアクセスランキング にほんブログ村

【Labs】JavaScriptで簡易CAPTCHA認証


【Labs】JavaScriptで簡易CAPTCHA認証

こんにちは(・∀・)

今日はJavaScriptで簡易CAPTCHA認証を作ってみましたのでご紹介します。

CAPTCHA認証

以前こちらのページでもご紹介したことがありますCAPTCHA認証のライブラリ(ページは削除済み)ですが、PHPのバージョンが変わって使えなくなっていて結構長い間放置プレイでしたが、そろそろどうにかしなきゃな、と夏休みの前半にコツコツとJavaScriptで作ってみました。

ランダムに出力された数字と入力した数字が一致したらtrueです。

JavaScriptなので切られていたら当然使えませんがないよりある方がいいだろう位のスタンスでいさせてください。

  1. 値を入力して認証するタイプ
  2. 表示された値と同じ値のチェックボックスをチェックして認証するタイプ
値を入力して認証するタイプ

メールフォームで使うことを想定してindex.htmlとmail.phpの2ファイル構成です。

index.html

<div class="tb-cell mail-form">
<form id="form" action="mail.php" method="post">
<div class="row">
<div class="cell">
<textarea type="text" name="output" id="output" value="" disabled></textarea><br>
<button type="button" name="que" id="que" >change</button>
<!--cell--></div>
<div class="cell">
<input type="text" name="ans" id="ans">
<p id="output2">値を入力してください</p>
<!--cell--></div>
<!--row--></div>
<div class="row">
<div class="cell">
&nbsp;
<!--cell--></div>
<div class="cell">
<button type="submit" id="sbtn" name="action" value="post">入力内容を確認</button>
<button type="reset" id="rbtn">リセット</button>
<!--cell--></div>
<!--row--></div>
</form>
<!--tb-cell--></div>
index.htmlのJavaScript

<script>
function randomString(){
var str = "0123456789";
var lenstr = 4;
var result = "";
for(var i=0;i<lenstr;i++){
result += str[Math.floor(Math.random()*str.length)];
document.getElementById('output').innerHTML = result;
}
}
window.addEventListener('DOMContentLoaded',randomString,false);
var sn = document.getElementById('que');
sn.addEventListener('click',randomString,false);
function captcha(){
question = output.value;
answer = ans.value;
target = document.getElementById("output2");
if(answer == ''){
target.innerHTML = '<p id="output3">入力してください</p>';
return false;
}else if(question != answer){
target.innerHTML = '<p id="output3">一致しません</p>';
return false;
}else if(question == answer){
target.innerHTML = '';
return true;
}
}
document.forms['form'].onsubmit = captcha;
function resetall() {	 	 
target.innerHTML = '<p id="output2">値を入力してください</p>';	 	 
return false;	 	 
}
var rbtn = document.getElementById('rbtn');
rbtn.addEventListener('click',resetall,false);
</script>
mail.php

<?php
$action = $_POST['action'];
if($action == "post"){
echo '<div class="tb-cell mail-form">';
echo '<form id="form" action="mail.php" method="post">';
echo '<div class="row">';
echo '<div class="cell">';
echo '&nbsp;';
echo '<!--cell--></div>';
echo '<div class="cell">';
echo '<p>認証されました</p><br>';
echo '<button type="button" onclick="history.go(-1)">入力フォームに戻る</button>';
echo '<!--cell--></div>';
echo '<!--row--></div>';
echo '</form>';
echo '<!--tb-cell--></div>';
}
?>
CSS

.tb-cell {
  display: table;
  width: 300px;
  margin: 20px auto 40px auto;
  text-align: left;
}
.tb-cell .row {
  display: table-row;
}
.tb-cell .row .cell {
  display: table-cell;
  border: 1px solid #ddd;
  padding: 10px;
  vertical-align: middle;
  color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
  width: 100px;
}
.tb-cell .row .cell:nth-child(even) {
  width: 200px;
}
.mail-form .row .cell {
  padding: 5px;
}
.mail-form .row .cell:nth-child(1) {
  background: #9fb7d4;
}
.mail-form .row .cell:nth-child(2) {
  background: #ccc;
}
input[type="text"],
input[type="email"] {
  height: 30px;
  font-size: 16px;
}
textarea {
  height: 100px;
  font-size: 16px;
}
button {
  color: #fff;
  border: none;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}
button[type="button"],
button[type="submit"] {
  background: #afc6e2;
}
button[type="reset"] {
  background: none;
}
button[type="button"]:hover,
button[type="submit"]:hover {
  background: #ddd;
}
button[type="reset"]:hover {
  text-decoration: underline;
}
.msg {
  margin: 50px 0;
  font-weight: normal;
  color: red;
}
label {
  margin-right: 10px;
}
#output {
  border: none;
  background: transparent;
  resize: none; 
  color: #fff;
  width: 50px;
  height: 20px;
  margin: 5px 0 10px 5px;
}
#output2 {
  color: #fff;
}
#output3 {
  color: #f00;
}
@media (min-width: 768px) {
.tb-cell {
  width: 800px;
}
.tb-cell .row {
  display: table-row;
}
.tb-cell .row .cell {
  display: table-cell;
  border: 1px solid #ddd;
  padding: 10px;
  vertical-align: middle;
  color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
  width: 200px;
}
.tb-cell .row .cell:nth-child(even) {
  width: 600px;
}
input[type="text"],
input[type="email"] {
  width: 300px;
}
textarea {
  width: 580px;
}
/* ** */}
Result

サンプルデモはこちら
スマホでのご確認はこちらをどうぞ
QRコード

表示された値と同じ値のチェックボックスをチェックして認証するタイプ

こちらもメールフォームで使うことを想定してindex.htmlとmail.phpの2ファイル構成です。

index.html

<div class="tb-cell mail-form">
<form id="form" action="mail.php" method="post">
<div class="row">
<div class="cell">
<textarea type="text" name="output" id="output" value="" disabled></textarea><br>
<button type="button" name="que" id="que" >change</button>
<!--cell--></div>
<div class="cell">
<label>a <input type="checkbox" name="checkbox" class="checkbox" id="valid1"></label>
<label>b <input type="checkbox" name="checkbox" class="checkbox" id="valid2"></label>
<label>c <input type="checkbox" name="checkbox" class="checkbox" id="valid3"></label>
<p id="output2">左に表示された値と同じ値を選択してください</p>
<!--cell--></div>
<!--row--></div>
<div class="row">
<div class="cell">
&nbsp;
<!--cell--></div>
<div class="cell">
<button type="submit" id="sbtn" name="action" value="post">入力内容を確認</button>
<button type="reset" id="rbtn">リセット</button>
<!--cell--></div>
<!--row--></div>
</form>
<!--tb-cell--></div>
index.htmlのJavaScript

<script>
function randomString(){
var str = "abc";
var lenstr = 1;
var result = "";
for(var i=0;i<lenstr;i++){
result += str[Math.floor(Math.random()*str.length)];
document.getElementById('output').innerHTML = result;
}
}
window.addEventListener('DOMContentLoaded',randomString,false);
var sn = document.getElementById('que');
sn.addEventListener('click',randomString,false);
function captcha(){
var question = output.value;
var v1=document.forms['form'].valid1.checked;
var v2=document.forms['form'].valid2.checked;
var v3=document.forms['form'].valid3.checked;
var a1= 'a';
var a2= 'b';
var a3= 'c';
target = document.getElementById('output2');
if(v1 == true){
if(a1 == question){
target.innerHTML = '';
return ture;
}else{
target.innerHTML = '<p id="output3">間違えてます</p>';
return false;
}
}else if(v2 == true){
if(a2 == question){
target.innerHTML = '';
return ture;
}else{
target.innerHTML = '<p id="output3">間違えてます</p>';
return false;
}
}else if(v3 == true){
if(a3 == question){
target.innerHTML = '';
return ture;
}else{
target.innerHTML = '<p id="output3">間違えてます</p>';
return false;
}
}else{
target.innerHTML = '<p id="output3">選択してください</p>';
return false;
}
}
document.forms['form'].onsubmit = captcha;
function resetall() {	 	 
target.innerHTML = '<p id="output2">左に表示された値と同じ値を選択してください</p>';	 	 
return false;	 	 
}
var rbtn = document.getElementById('rbtn');
rbtn.addEventListener('click',resetall,false);
function checkbox(){
document.forms['form'].valid1.onclick=ckbox1;
document.forms['form'].valid2.onclick=ckbox2;
document.forms['form'].valid3.onclick=ckbox3;
}
function ckbox1(){
document.forms['form'].valid2.checked=false;
document.forms['form'].valid3.checked=false;
}
function ckbox2(){
document.forms['form'].valid1.checked=false;
document.forms['form'].valid3.checked=false;
}
function ckbox3(){
document.forms['form'].valid1.checked=false;
document.forms['form'].valid2.checked=false;
}
window.addEventListener('DOMContentLoaded',checkbox,false);
window.addEventListener('DOMContentLoaded',checkbox,false);
window.addEventListener('DOMContentLoaded',checkbox,false);
</script>
mail.php

<?php
$action = $_POST['action'];
if($action == "post"){
echo '<div class="tb-cell mail-form">';
echo '<form id="form" action="mail.php" method="post">';
echo '<div class="row">';
echo '<div class="cell">';
echo '&nbsp;';
echo '<!--cell--></div>';
echo '<div class="cell">';
echo '<p>認証されました</p><br>';
echo '<button type="button" onclick="history.go(-1)">入力フォームに戻る</button>';
echo '<!--cell--></div>';
echo '<!--row--></div>';
echo '</form>';
echo '<!--tb-cell--></div>';
}
?>
CSS

.sample-demo {
 text-align: center;
}
.tb-cell {
  display: table;
  width: 300px;
  margin: 20px auto 40px auto;
  text-align: left;
}
.tb-cell .row {
  display: table-row;
}
.tb-cell .row .cell {
  display: table-cell;
  border: 1px solid #ddd;
  padding: 10px;
  vertical-align: middle;
  color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
  width: 100px;
}
.tb-cell .row .cell:nth-child(even) {
  width: 200px;
}
.mail-form .row .cell {
  padding: 5px;
}
.mail-form .row .cell:nth-child(1) {
  background: #9fb7d4;
}
.mail-form .row .cell:nth-child(2) {
  background: #ccc;
}
input[type="text"],
input[type="email"] {
  height: 30px;
  font-size: 16px;
}
textarea {
  height: 100px;
  font-size: 16px;
}
button {
  color: #fff;
  border: none;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}
button[type="button"],
button[type="submit"] {
  background: #afc6e2;
}
button[type="reset"] {
  background: none;
}
button[type="button"]:hover,
button[type="submit"]:hover {
  background: #ddd;
}
button[type="reset"]:hover {
  text-decoration: underline;
}
.msg {
  margin: 50px 0;
  font-weight: normal;
  color: red;
}
label {
  margin-right: 10px;
}
#output {
  border: none;
  background: transparent;
  resize: none; 
  color: #fff;
  width: 50px;
  height: 20px;
  margin: 5px 0 10px 5px;
}
#output2 {
  color: #fff;
}
#output3 {
  color: #f00;
}
@media (min-width: 768px) {
.tb-cell {
  width: 800px;
}
.tb-cell .row {
  display: table-row;
}
.tb-cell .row .cell {
  display: table-cell;
  border: 1px solid #ddd;
  padding: 10px;
  vertical-align: middle;
  color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
  width: 200px;
}
.tb-cell .row .cell:nth-child(even) {
  width: 600px;
}
input[type="text"],
input[type="email"] {
  width: 300px;
}
textarea {
  width: 580px;
}
/* ** */}
Result

サンプルデモはこちら
スマホでのご確認はこちらをどうぞ
QRコード

関連リンク

【Labs】PHPでシンプルなメールフォーム


にほんブログ村 デザインブログ Webデザインへ PVアクセスランキング にほんブログ村