ajax提交表单 验证
1 function submitKH(mobileInputId,nameInputId) { 2 var mobileInputSelector ='#'+ mobileInputId; 3 var passwordInputIdInputSelector = '#'+ passwordInputId; 4 var nameInputIdInputSelector = '#'+ nameInputId; 5 var mobile = jQuery(mobileInputSelector).val(); 6 var name = jQuery(nameInputIdInputSelector).val(); 7 var password = jQuery(passwordInputIdInputSelector).val(); 8 var localUrl = document.location; 9 var source="wap"; 10 var type="开户"; 11 if (name.length == 0||name=="请输入您的姓名") { 12 alert("您的姓名不能为空!"); 13 jQuery(nameInputIdInputSelector).focus(); 14 return false; 15 } 16 if (password.length == 0||password=="输入您的密码") { 17 alert("您的密码不能为空!"); 18 jQuery(passwordInputSelector).focus(); 19 return false; 20 } 21 if (mobile.length == 0||mobile=="请输入您的手机号码") { 22 alert("您的手机号码不能为空!"); 23 jQuery(mobileInputSelector).focus(); 24 return false; 25 } 26 27 if (mobile.length > 0) { 28 var ab = /^(13[0-9]|15[0-9]|18[0-9])\d{8}$/; 29 if (ab.test(mobile) == false) { 30 alert("您的手机号码不正确!"); 31 jQuery(mobileInputSelector).focus(); 32 return false; 33 } 34 } 35 jQuery.ajax({ 36 type: "get", 37 url: "/KZH/AddNewKH", 38 data: "mobile=" + mobile +"&password="+password+ "&name="+name+"&source="+source+"&type=" + type + "&t=" + new Date().toString(), 39 dataType: "jsonp", 40 success: function (result) { 41 if (result.success == true) { 42 jQuery(mobileInputSelector).val(""); 43 jQuery(passwordInputSelector).val(""); 44 jQuery(nameInputIdInputSelector).val(""); 45 alert("恭喜您,提交成功!"); 46 $("#ycbox").css('display','block'); 47 setCookie('name',name); 48 setCookie('phone',mobile); 49 } 50 else if(result.success == false) 51 { 52 alert("您已预约成功,无需重复提交,客服会尽快与您取得联系!"); 53 $("#ycbox").css('display','block'); 54 jQuery(mobileInputSelector).val(""); 55 jQuery(passwordInputSelector).val(""); 56 jQuery(nameInputIdInputSelector).val(""); 57 } 58 } 59 }); 60 }
调用:
submitKH('mobile','password','name'); return false;