java 手机验证码
生活随笔
收集整理的這篇文章主要介紹了
java 手机验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要一個做批量轉賬的功能,為了賬戶安全,執行該操作前需要向指定手機號碼發送驗證碼,驗證碼一致才可以進行之后的操作
jsp頁面:
<th>獲取手機驗證碼:</th><td><input name="phoneCode" class="easyui-validatebox" required="true" style="width: 150px;"><a href="javascript:void(0);" id='btn' class="easyui-linkbutton" οnclick="getValidationCode();">獲取驗證碼</a></td>
function sendValidateCode() {settime();$.ajax({url : '',type : 'post',cache : false,dataType : 'json',success : function(returnData) {returnData = eval("(" + returnData + ")");if (returnData && returnData.code == "0") {$.messager.alert('標題', returnData.message);} else {$.messager.alert('標題', returnData.message);}},error : function() {$.messager.alert('標題',"網絡異常");}});}settime():
var countdown = 60;function settime() {if (countdown == 0) {$('#btn').linkbutton('enable'); $("#btn").text("獲取驗證碼");countdown = 60;return;} else {$('#btn').linkbutton('disable'); $("#btn").text("重新發送(" + countdown + ")");countdown--;}t = setTimeout(function() {settime()}, 1000)}因為這是放在一個表單里面,可能會出現的問題就是關閉了表單再打開,獲取驗證碼按鈕上面的時間還是從關閉之前的技術的
需要在關閉的時候
?clearTimeout(t); //將時間清空
$('#btn').linkbutton('enable'); ?//按鈕可用
$("#btn").text("獲取驗證碼");//按鈕文字
java后臺將驗證碼保存到session中并通過短信接口發送短信:
//生成6位隨機驗證碼StringBuffer code = new StringBuffer(); for (int i = 0; i < 6; i++) {Random random = new Random();code.append(random.nextInt(10));}//保存到session中request.getSession().setAttribute("code", code.toString());調用短信接口將短信發送
用戶在提交表單的時候驗證短信驗證碼
//獲取驗證碼并驗證是否一致String sessionCode = (String)request.getSession().getAttribute("code");if(sessionCode == null ||"".equals(sessionCode)){return MessageUtil.operationSuccess("請重新獲取驗證碼!");}if(!sessionCode.equals(phoneCode)){return MessageUtil.operationSuccess("驗證碼不一致!");}//驗證之后刪除session中的驗證碼request.getSession().removeAttribute("code");驗證時候刪除session中的驗證碼是為了防止重復走下面的流程。短信驗證碼到這里就結束了,接下來就是其他驗證及業務流程。
總結
以上是生活随笔為你收集整理的java 手机验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rdl报表折叠设置
- 下一篇: win7与ubuntu双系统,启动err