1. 程式人生 > >js驗證登錄註冊

js驗證登錄註冊

AD click 登陸 parseint 隨機 整數 int .html ima

js驗證登錄註冊的優勢,在前臺直接驗證,不需要在後臺讀取返回數據驗證,減輕服務器壓力。

登陸驗證得必要性,攔截惡意腳本的登錄註冊攻擊。

js代碼如下:

$(document).ready(function(){
var result=0;
var show="";
var inresult;
var tag=false;
getArithmetic();

/**
* 改變驗證碼算術式
*/
$(document).on("click","#sswitch",function(){
getArithmetic();
});

/**
*
*/
$(document).on("blur","#result",function(){
inresult=$("#result").val();
if(result!=inresult){
$("#result").parent().next().html("");
$("#result").parent().next().html("答案錯誤");
}
if(inresult==""){
$("#result").parent().next().html("");
$("#result").parent().next().html("答案不能為空!");
}
if(inresult==result){
$("#result").parent().next().html("");
$("#result").parent().next().html("驗證通過!");
tag=true;
}
});
/**
* 生成驗證算術式
*/
function getArithmetic() {
tag=false;
var a=parseInt(Math.random()*10);//生成0-10的隨機數,可以生成到100增加計算難度
var b=parseInt(Math.random()*10);
var n1=Math.floor(Math.random()*3+1);//輸出1~4之間的隨機整數,省去除法,所以改成3
if(n1==1){
result=a+b;
show=a+" + "+b+"=";
}
if(n1==2){
result=a-b;
show=a+" - "+b+"=";
}
if(n1==3){
result=a*b;
show=a+" * "+b+"=";
}
/* if(n1==4){
result=a/b;
show=a+"/"+b+"=";
alert(result);
}*/
$("#show").val(show);
$("#show").html(show);
}
});
頁面效果:

技術分享圖片

js驗證登錄註冊