首页 » 技术分享 » ajax同步请求 和 表单验证submitHandler

ajax同步请求 和 表单验证submitHandler

 

ajax异步请求可能会改变程序的运行顺序,今天接触来这个问题,请外也使用了submitHandler进行表单提交前的验证:

<script type="text/javascript">
var countMember = 0;
$().ready(function() {

var $inputForm = $("#inputForm");

[@flash_message /]

// 表单验证
$inputForm.validate({
rules: {
dictClassId: {
required: true
},
studentNo: {
maxlength: 15,
pattern: /^\w+$/,
remote: {
url: "check_studentNo.ct",
cache: false
}
},
studentName: {
required: true,
maxlength: 20
},
success: function() {
layer.load();
}
},
messages: {
studentNo: {
remote: "${message("console.validate.exist ")}"
}
},
submitHandler: function(form) {
var isMobile = true;
var isRealName = true;
var isRepeat = true;
var isCards=true;
var pattern = /^1[345678]\d{9}$/;

if (countMember == 0) {
if ($('.search-choice').length == 0) {
$.message("warn", "至少添加一个家长!");
return false;
}
};

//手机号码校验
$(".mobile").each(function() {
var mobile = $(this).val().replace(/^\s+|\s+$/g, "");
if (mobile == "") {
$.message("warn", "请填写家长手机号!");
$(this).focus();
isMobile = false;
return false;
} else {
if (!pattern.test(mobile)) {
$.message("warn", "手机号" + mobile + "格式不正确!");
$(this).focus();
isMobile = false;
return false;
} else {
$.ajax({
type: "GET",
url: "add_check_mobileNumber.ct",
data: {
mobile: mobile
},
dataType: "json",
async: false,
success: function(existMobile) {
if (existMobile != "") {
$.message("warn", "手机号" + existMobile + "已经存在!");
$(this).focus();
isMobile = false;
return false;
}
},
});
}
}
});

//家长姓名必填
if (isMobile == true) {
$(".realName").each(function() {
var realName = $(this).val().replace(/^\s+|\s+$/g, "");
if (realName == "") {
$.message("warn", "请填写家长姓名!");
isRealName = false;
$(this).focus();
return false;
}
});
}

//校验卡号
$(".timeCards").each(function() {
var timeards = $(this).val().replace(/^\s+|\s+$/g, "");
var pattern = /^(\d{10})(,\d{10})*$/;
if (timeards != "") {
if (!pattern.test(timeards)) {
$.message("warn", "卡号格式不正确,多卡请用中文“,”分隔!");
$(this).focus();
isCards=false;
return false;
}
$.ajax({
type: "GET",
url: "add_check_timeCards.ct",
data: {
timeards: timeards
},
dataType: "json",
async: false,
success: function(existCard) {
if (existCard != "") {
$.message("warn", "卡号" + existCard + "已经存在!");
$("#submitBtn").attr("disabled", true);
$(this).focus();
isCards=false;
return false;
}
}
});
}
});

//校验手机号码重复填写
$("input[name='mobile']").each(function() {
var omobile = $(this).val().replace(/^\s+|\s+$/g, "");
var count = 0;
$("input[name='mobile']").each(function() {
var imobile = $(this).val().replace(/^\s+|\s+$/g, "");
if (imobile != "" && imobile == omobile) {
count++;
}
});
if (count > 1) {
$.message("warn", "请勿重复填写手机号:" + omobile);
isRepeat = false;
return false;
}
});

if (isMobile == true && isRealName == true && isRepeat == true && isCards == true) {
form.submit();
}
}
});

//新增家长
$("#addMemberBtn").click(function(){
countMember++;
var memberListHTML = '

'
+'

'
+'

*${message("手机号")}:

'
+'

'
+''
+'

'
+'
'
+'

'
+'

*   ${message("姓名")}:

'
+'

'
+''
+'

'
+'
'
+'

'
+'

     ${message("卡 号")}:

'
+'

'
+''
+'

'
+'
'
+'

'
+'

${message("亲属关系")}:

'
+'

'
+'
  • ' +'
  • ' +'
  • ' +'
  • ' +'
  • ' +'
  • ' +''
    +'

    删除

    转载自原文链接, 如需删除请联系管理员。

    原文链接:ajax同步请求 和 表单验证submitHandler,转载请注明来源!

    0
    相关推荐
    Copyright © 2020 | SEO分享博客 | 冀ICP备15004514号-2 | 网站地图 HMJ-Blog Theme by 何敏杰