Ajax+Jquery请求(获取文本值和相关验证)

function addUsers() {

var urlStr = "/ajax/home.action?op=User.addUsers";

var pwdText = $("#pwdText").val();

if(isNullStr(pwdText)){
alert("密码不能为空");
return;
}

var pwdAginText = $("#pwdAginText").val();
if(isNullStr(pwdAginText)){
alert("确认密码不能为空");
return;
}
if(pwdText!=pwdAginText){
alert("密码与确认密码不一致");
return;
}
var userAccountText = $("#userAccountText").val();
if(isNullStr(userAccountText)){
alert("账号不能为空");
return;
}
var urlExtisAddStr = "/ajax/home.action?op=User.existAcc";
var existDataStr = "accountText=" + userAccountText;
$.ajax({
url : urlExtisAddStr  + "&tz=" + Math.random(),
dataType : "json",
data : existDataStr,
async : false,
cache : false,
success : function(json) {
if (json.existAccResult == 1) {
var dept = document.getElementById("dept");
var deptId = dept[dept.selectedIndex].value;
if(deptId<1){
alert("请选择正确的部门");
return;
}
var role = document.getElementById("role");
var roleId = role[role.selectedIndex].value;
if(roleId<1){
alert("请选择正确的角色");
return;
}

var dataStr = "accountText=" + userAccountText;

dataStr = dataStr + "&userPwd=" + pwdText;
var userNameText = $("#userNameText").val();
if(isNullStr(userNameText)){
alert("用户姓名不能为空!");
}
dataStr = dataStr + "&accNameText=" + userNameText;
var sexvalu=0;
var sexradio=document.getElementsByName("sexradio");
for(var i = 0;i<sexradio.length;i++){
if(sexradio[i].checked){

sexvalue = sexradio[i].value;
break;
}
}
dataStr = dataStr + "&userSex=" + sexvalue;
var role = document.getElementById("role");
dataStr = dataStr + "&roleId=" + role[role.selectedIndex].value;
var dept = document.getElementById("dept");
dataStr = dataStr + "&deptId=" + dept[dept.selectedIndex].value;
var extensionText = $("#extensionText").val();
dataStr = dataStr + "&extension=" + extensionText;
var phoneText = $("#phoneText").val();

if(isNullStr(phoneText)) {
alert("手机号码不许为空!");
return;
}
if(!(/^1[3|5][0-9]\d{4,8}$/.test(phoneText))){
          alert("不是正确的11位手机号");
          return false;
      }else{
      dataStr = dataStr + "&userPhone=" + phoneText;
    }



var userBirthdayText = $("#userBirthday").val();
dataStr = dataStr + "&userBirthday=" + userBirthdayText;
if(isNullStr(userBirthdayText)){
alert("用户出生日期不正确,请重新填写!");
return;
}
var addressText = $("#addressText").val();
dataStr = dataStr + "&userAddress=" + addressText;
var qqText = $("#qqText").val();
dataStr = dataStr + "&userQQ=" + qqText;
var msnText = $("#msnText").val();
dataStr = dataStr + "&userMSN=" + msnText;
var emailText = $("#emailText").val();
if(checkemail(emailText)){
dataStr = dataStr + "&userEmail=" + emailText;
}
else{
return;
}
var telText = $("#telText").val();


if(isNullStr(telText)) {
alert("电话号码不许为空!");
return;
}
var isIDcard = /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])d{8}$)/;
if (!isIDcard.test(telText)) {
alert("固定电话输入错误,请重新输入!");
return false;
}else{
dataStr = dataStr + "&userTel=" + telText;
    }

var limitIpText = $("#limitIpText").val();
dataStr = dataStr + "&userLimitIp=" + limitIpText;
var userRecord=-1;
var recordradio=document.getElementsByName("recordradio");
for(var i = 0;i<recordradio.length;i++){
if(recordradio[i].checked){

userRecord = recordradio[i].value;
break;
}
}
dataStr = dataStr + "&userRecord=" + userRecord;
var rollInTypeText = $("#rollInTypeText").val();
dataStr = dataStr + "&rollInType=" + rollInTypeText;
$.ajax({
url : urlStr  + "&tz=" + Math.random(),
dataType : "json",
data : dataStr,
async : false,
cache : false,
success : function(json) {
if (json.addUserResult > 0) {
alert("添加用户成功!");
minesUpdate('home.action?op=User.usersManager');
} else {
//结果为空,小于0
alert("添加用户失败!");
}
return;
},
error : function(msg) {
alert("添加用户失败!");
}
});
}else{
alert("账号已存在,请换个重试!");
return;
}
},
error : function(msg) {
alert("账号验证失败!");
return;
}
});
}

你可能感兴趣的:(Jquery+ajax)