手機wap端註冊jQuery Ajax呼叫API介面
阿新 • • 發佈:2019-02-06
var mobileEl = $('#mobile'),
passwordEl = $('#password'),
captchaEl = $('#yzm'),
urlEl = "http://803.dev.devbao.cn/";
var countdown = false; // 是否正在倒計時
// 是否正確的手機號
function checkForm() {
var mobile = mobileEl.val();
var pwd = passwordEl.val();
console.info("ss" ,isPassword(pwd),pwd);
if(mobileEl.val()==""){
alert("請輸入手機號碼");
return false;
}else if(!isMobile(mobile)){
alert("請輸入正確的手機號碼");
return false;
}else if(!isPassword(pwd)){
alert("請輸入6-16位密碼!");
return false;
}else if(captchaEl.val().length<=0 || captchaEl.val().length>6){
alert("請輸入正確的驗證碼!");
return false;
}else
return false;
}
$("#captchaBtn").click(function () {
var mobile = mobileEl.val();
if(!isMobile(mobile)){
alert("請輸入正確的手機號碼");
return false;
}else
getCaptcha(mobile)
});
/**
* 驗證手機號
* @param value
* @returns {boolean}
*/
function isMobile(value) {
var reg = /^1[0-9]{10}$/;
return reg.test(value);
}
/**
* 驗證密碼
* @param value
* @returns {RegExp|boolean}
*/
function isPassword(value) {
return /^[\u0000-\u00ff]{6,16}$/.test(value)
}
/**
* 獲取驗證碼
* @param mobile
*/
function getCaptcha(mobile) {
if(countdown)
return;
$.ajax({
type: "POST",
url: urlEl+"/promote/landing/sendRegSms",
data: {
phone:mobile
},
success: function(msg){
if(msg=='false-a'){
alert("傳送驗證碼失敗:驗證碼為空");
} else if(msg=='false-b'){
alert("傳送驗證碼失敗:驗證碼錯誤");
} else if(msg=='false-c'){
alert("傳送驗證碼失敗:該手機已經註冊");
} else if(msg=='false-d'){
alert("傳送驗證碼失敗:你傳送得太頻繁啦");
} else if(msg=='false-e') {
alert("傳送驗證碼失敗");
}else{
showTime();
}
},
error:function(){
alert('error');
}
});
}
function showTime() {
var t = 60;
countdown = true;
var timer = setInterval(function() {
$("#captchaBtn").html(t + "s");
if(t<0){
t = 60;
clearInterval(timer);
$("#captchaBtn").html("重新發送");
countdown = false;
}
t -= 1;
},1000)
}
//顯示密碼按鈕
$('#eyeBtn').click(function() {
var type = passwordEl.attr('type');
passwordEl.attr({'type': type == 'password' ? 'text' : 'password'})
})