当前位置: 移动技术网 > IT编程>开发语言>Jquery > 短信提交验证

短信提交验证

2018年12月10日  | 移动技术网IT编程  | 我要评论

//-------------------------------------------短信提交验证------------------------------------------//
$(function(){
var daoshu = 60;
var timer = null;
//手机号
function checkphone(){
var phonenumber = $.trim($('#t_code').val());
var phone = document.getelementbyid('t_code').value;
var smscode = $.trim($('#smscode').val());
if (phonenumber == null || phonenumber == "") {
//com.prompt('请输入手机号!');
$('#yaerror').show();
$('#yaerror span').html('请输入手机号');
return false;
//手机号正则
}else if(!(/^1[3456789]\d{9}$/.test(phone))) {
//com.prompt('手机号格式错误');
$('#yaerror').show();
$('#yaerror span').html('手机号格式错误');
return false;
}
return true;
}

//手机验证码
function checksmscode(){
var smscode = $.trim($('#smscode').val());
if (smscode == null || smscode == "") {
if(parent.location.href.indexof('reg') >= 0){
// com.prompt('请输入验证码');
$('#yaerror').show();
$('#yaerror span').html('请输入验证码');
}else{
// com.prompt('请输入验证码');
$('#yaerror').show();
$('#yaerror span').html('请输入验证码');
}
return false;
}
return true;
}
//发送短信验证码
$(".send-sms").click(function(){
var mobile = $.trim($('#t_code').val());
//console.log(typeof mobile);

if(!mobile){
// com.prompt("请输入手机号");
return false;
}
if(!checkphone()){
return false;
}

send_login_code(mobile);
});
//获取验证码倒计时1分钟
function fn_daoshu(){
$(".daoshu").show();
daoshu -- ;
$(".daoshu").text(daoshu +'s');
if(daoshu == 0){
clear_inter();
}
}
//清楚定时器
function clear_inter(){
daoshu = 60;
$(".daoshu").text('60s').hide();
clearinterval(timer);
}
//发送短信验证码
function send_login_code(mobile)
{

$(".daoshu").show();
var code;
if($("#get_code").css("display") == 'none'){
code = 'is_empty';
}else {
if($('#yz_code').val() == ''){
com.prompt('请输入图形验证码');
return false;
}
code = $('#yz_code').val();
}
timer = setinterval(fn_daoshu,1000);
$("#smscode").attr("disabled" , false);
$("#smscode").attr("disabled" , false);
$.ajax({
type:"post",
url:com.url.get_sms_code,
data:{
mobile: mobile,
},
datatype: "json",
success:function(res){
if(res.status == 'true'){
com.prompt("发送成功");
// $('#yaerror').show();
// $('#yaerror span').html('发送成功!');
}else{
com.prompt(res.info);
clear_inter();
return false;
}
},
error: function(res){
//com.prompt('请重新获取');
$('#yaerror').show();
$('#yaerror span').html('请重新获取');
clear_inter();
return false;
// console.log(res)
}
});
}

//用户名
$("#t_usrname").focus(function(){
if($(this).val().length>0){
$('#userdelico').show();
}

})
$("#t_usrname").blur(function(){
var username = $('#t_usrname').val();
if(username == '') return false;
//只能输入中文 英文 和空格
var reg =/^[a-za-z\u4e00-\u9fa5\s ]{1,20}$/;
if(!(reg.test(username))) {
//com.prompt('姓名格式不正确');
$('#yaerror').show();
$('#yaerror span').html('姓名格式不正确');
return false;
}
// $('#userdelico').hide();
return true;
});

//手机验证码
function t_usrname(){
var username = $('#t_usrname').val();
var reg =/^[a-za-z\u4e00-\u9fa5\s ]{1,20}$/;
if(username == null || username == "") {
return true;
}else if(username!=''){
if(!(reg.test(username))) {
//com.prompt('姓名格式不正确');
$('#yaerror').show();
$('#yaerror span').html('姓名格式不正确');
return false;
}
}
return true;
}
$('#t_usrname').on("input",function(){
var username = $.trim($('#t_usrname').val());
if(username.length>0){
$(this).parent('.posiv').siblings('.delico').show();
$(this).parent('.posiv').siblings('.delico').click;
}
})
//手机号焦点校验
$('#t_code').on("focus",function(){
if($(this).val().length>0){
$('#teldelico').show();
}
//已登录状态
if(parseint(is_login)>'0' && parseint(need_bind_mobie)>'1') {
$('#seleid').show(400);
$('#seleid').html(need_bind_mobie)
document.queryselector('#seleid').onclick=showmobile;
}else{

$('.flp.vufk').show();
}
})
function showmobile(){
$('#t_code').val(this.innerhtml)
$('#seleid').hide();
$('.flp.vufk').hide(400);
$('#yaerror').hide();
}
//已登录
if(parseint(is_login)>'0' && parseint(need_bind_mobie)>'1'){
$('#tel_msg').click(function() {
var phone = $.trim($('#t_code').val());//手机号
var user_name = $.trim($("#t_usrname").val());//用户名
var source = $.trim($("#source").val()); //暂时写死
//换号了
if(phone!=need_bind_mobie){
console.log('换号了')
$('#yusuc').hide();
$('.flp.vufk').show(400);
$('#sendsms_box').show();
nologin();
}else{
$('.flp.vufk').hide(400);
console.log('没换号')
//没换号
var userinfo = {
user_name: user_name,
mobile: phone, //手机号
source:source,
pf_source:2,
ip: returncitysn["cip"]

};
if(!t_usrname()){
return false;
}
if(!checkphone()){
return false;
}

sendapidata({reg_source:userinfo.source,mobile:userinfo.mobile,name:userinfo.user_name,pf_source:2,ip: returncitysn["cip"]});
}
})
}else{
console.log('没登录')
$('.flp.vufk').show();
$('#tel_msg').click(function(){
//没登录
nologin();
})
}
//手机号失焦校验
$('#t_code').blur(function(){
if(!checkphone()){
return false;
}
})
//正在输入检验
$('#t_code').on("input",function(){
var phonenumber = $.trim($('#t_code').val());
if(parseint(is_login)>'0' && parseint(need_bind_mobie)>'1'){
if(phonenumber!=need_bind_mobie){
$('#seleid').hide();
$('.flp.vufk').show(400);
}
if(phonenumber==need_bind_mobie){
$('.flp.vufk').hide(400);
}
}else {
$('.flp.vufk').show();
}


if(phonenumber.length>0){
$(this).parent('.posiv').siblings('.delico').show();
$(this).parent('.posiv').siblings('.delico').click;
}
var phone = document.getelementbyid('t_code').value;
var smscode = $.trim($('#smscode').val());
if (phonenumber == null || phonenumber == "") {
$(this).parent('.posiv').siblings('.delico').hide();
$(".send-sms").css({"background":"#eee","color":"#999"});
// $(".send-sms").attr('disabled','disabled');
return false;
//手机号正则
}else if(!(/^1[3456789]\d{9}$/.test(phone))) {
$(".send-sms").css({"background":"#eee","color":"#999"});
return false;
}
$(".send-sms").css({"background":"#ff5922","color":"#fff"});
$(".send-sms.dbing").css({"background":"#b41f5e","color":"#fff"});
$('#yaerror').hide();
$('#yaerror span').html('');
$('.flp.vufk').show();
})
$('#userdelico').click(function(){
$(this).siblings('.posiv').find('.ptxt').val('');
// $('#t_usrname').val('');
clear_inter();
$(this).hide();
$('.test_required').hide();
if($('#yaerror span').html()=='姓名格式不正确'){
$('#yaerror').hide();
if(!checkphone()){
return false;
}
}
})

$('#teldelico').click(function(){
$(this).siblings('.posiv').find('.ptxt').val('');
$(".send-sms").css({"background":"#eee","color":"#999"});
$("#smscode").attr('disabled','disabled').val('');
clear_inter();
$(this).hide();
$('.test_required').hide();
if($('#yaerror span').html()=='手机号格式错误'){
$('#yaerror').hide();
}
if($('#yaerror span').html()=='请输入验证码'){
$('#yaerror').hide();
}
if($('#yaerror span').html()=='验证码错误'){
$('#yaerror').hide();
}
})

//没登录做验证
function nologin(){
var phone = $.trim($('#t_code').val());
var source = $.trim($("#source").val()); //暂时写死
var yz_code = $.trim($('#smscode').val());
var user_name =$.trim($("#t_usrname").val());
var userinfo = {
user_name:user_name,
mobile : phone, //手机号
code : yz_code , //验证码
source : source //来源渠道
};
if(!t_usrname()){
return false;
}
if(!checkphone()){
return false;
}
if(!checksmscode()){
return false;
}
mobile_submit(userinfo);
}

//手机号登陆start
function mobile_submit(userinfo){
$.ajax({
type:"post",
url: com.url.check_sms_code,
data:{
user_name:userinfo.user_name,
mobile : userinfo.mobile,
code : userinfo.code,
source : userinfo.source
},
datatype: 'json',
success: function(res){
if(res.status == 'true'){
// com.prompt('已提交');
$('#yaerror').show();
$('#yaerror span').html('已提交');
$('#t_code').val('');
$('#t_usrname').val('');
clear_inter();
$("#smscode").attr('disabled','disabled').val('');
$(".send-sms").css({"background":"#eee","color":"#999"});
$('.delico').hide();
$('#yaerror').hide();
$('#yaerror').html('');

sendapidata({reg_source:userinfo.source,mobile:userinfo.mobile,name:userinfo.user_name,pf_source:2,ip: returncitysn["cip"]});

$('#yusuc').show();
$('#sendsms_box').hide();
$('.module_10 .sendheight').css('height','14rem');
$('.pic_online10').css('height','17rem');
$('.ddt.last_getuser').css('height','11rem');
$('.no9,.last_getuser.fsd').css('height','12rem');
$('.index_apply .twrap06').css('height','12.3rem');

}else{
//com.prompt("验证码错误");
$('#yaerror').show();
$('#yaerror span').html('验证码错误');
}
},
error: function(res){
// com.prompt('请重新提交');
$('#yaerror').show();
$('#yaerror span').html('请重新提交!');
// console.log(res)
}
})
}

//手机号登陆start,最终提交代码
function sendapidata(userinfo){
$.ajax({
type:"get",
url: ucse_host+'/xuemao/formcollection/save_form_data.json',
data:userinfo,
xhrfields: {
withcredentials: true
},
datatype: 'json',
success: function(res){
if(res.result == 'succ'){
$('#yusuc').show();
$('#sendsms_box').hide();
$('.module_10 .sendheight').css('height','14rem');
$('.pic_online10').css('height','17rem');
$('.ddt.last_getuser').css('height','11rem');
$('.no9,.last_getuser.fsd').css('height','12rem');
$('.index_apply .twrap06').css('height','12.3rem');

}else{
//com.prompt("验证码错误");
$('#yaerror').show();
$('.vufk .error').show();
}
},
error: function(res){
//com.prompt('请重新提交');
$('#yaerror').show();
$('#yaerror span').html('请重新提交');

// console.log(res)
}
})
}
//点击其他位置隐藏
$('body').click(function(e) {
var target = $(e.target);
if(!target.is('#t_code') && !target.is('#seleid')) {
$('#seleid').hide();
}
});

$('body').click(function(e) {
var target = $(e.target);
if(!target.is('#t_usrname')) {
$('#userdelico').hide();
}
});
$('body').click(function(e) {
var target = $(e.target);
if(!target.is('#t_code')) {
$('#teldelico').hide();
}
});
})

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网