驗(yàn)證碼的生成
1,基本資訊的驗(yàn)證
<?php $(document).ready(function() { $("#yzmfs").click(function () { //確保手機(jī)號(hào)不為空 var mobile=$("#phone").val(); if(mobile.length==0) { alert('請(qǐng)輸入手機(jī)號(hào)碼!'); $("#phone").focus(); return false; } if(mobile.length!=11) { alert('請(qǐng)輸入11位手機(jī)號(hào)!'); $("#phone").focus(); return false; } var myreg = /^((1[3|4|5|8][0-9]{1})+\d{8})$/; if(!myreg.test(mobile)) { alert('請(qǐng)輸入正確的手機(jī)號(hào)碼!'); document.getElementById("phone").focus(); return false; } //點(diǎn)擊發(fā)送短信驗(yàn)證碼 }) })
2,點(diǎn)擊發(fā)送簡(jiǎn)訊驗(yàn)證碼出現(xiàn)60秒倒數(shù)的js實(shí)作:
<script type="text/javascript"> var countdown=60; function settime(obj){ //60秒倒計(jì)時(shí) if (countdown == 0){ obj.removeAttribute("disabled"); obj.value="發(fā)送短信驗(yàn)證碼"; countdown = 60; return; }else{ obj.setAttribute("disabled", true); obj.value="重新發(fā)送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(obj) } ,1000) } </script>
3,ajax實(shí)作驗(yàn)證碼的產(chǎn)生
先引進(jìn)jquery檔
<script src="jquery -1.11.0.js" type="text/javascript"></script>
##
<?php //點(diǎn)擊發(fā)送短信驗(yàn)證碼 $.ajax({ async : false, type: "get", url: "code.php", // data: {}, success: function (data) { $("#code").val(data); } });
<?php $code_len=4; $code=array_merge(range('A','Z'),range('a','z'),range(1,9));//需要用到的數(shù)字或字母 $keyCode=array_rand($code,$code_len);//真正的驗(yàn)證碼對(duì)應(yīng)的$code的鍵值 if($code_len==1){ $keyCode=array($keyCode); } shuffle($keyCode);//打亂數(shù)組 $verifyCode=""; foreach ($keyCode as $key){ $verifyCode.=$code[$key];//真正驗(yàn)證碼 } echo base64_encode($verifyCode);###### ####