//https://validator.niceue.com/docs/options.html#section-2-7
//版本最后更新日期:2018-01-30
; var ajaxpagemethod = {
/*调用cs后端服务器方法(static静态方法)-同步执行*/
postsync: function (ajaxurl, postdata, isloading) {
//var returnjson = { "id": "2016/11/23 15:24:47", "text": "", "status": "empty" };
var returnjson = '';
if (ajaxurl.indexof("?") == -1) { ajaxurl = ajaxurl + "?_=" + math.random(); }
if (postdata == undefined) { postdata = {}; }
$.ajax({
type: 'post',
cache: false,
async: false,//false代表只有在等待ajax执行完毕后才执行下面语句
url: ajaxurl,
data: json.stringify(postdata),
//contenttype: "application/x-www-form-urlencoded; charset=utf-8",
contenttype: "application/json;charset=utf8",
datatype: 'json',
beforesend: function (xmlhttprequest) {
if (isloading == true) { layer.load(2); }
},
success: function (data) {
returnjson = $.parsejson(data.d);
if (isloading == true) { layer.closeall("loading"); }
},
error: function (err) {
//console.log(err);
},
complete: function (xmlhttprequest, textstatus) {
if (isloading == true) { layer.closeall("loading"); }
}
});
return returnjson;
},
/*调用cs后端服务器方法(static静态方法)-异步执行*/
postasync: function (ajaxurl, postdata, callback, isloading) {
if (ajaxurl.indexof("?") == -1) { ajaxurl = ajaxurl + "?_=" + math.random(); }
if (postdata == undefined) { postdata = {}; }
$.ajax({
type: 'post',
cache: false,
async: true,//false代表只有在等待ajax执行完毕后才执行下面语句
url: ajaxurl,
data: json.stringify(postdata),
//contenttype: "application/x-www-form-urlencoded; charset=utf-8",
contenttype: "application/json;charset=utf8",
datatype: 'json',
beforesend: function (xmlhttprequest) {
if (isloading == true) { layer.load(2); }
},
success: function (data) {
data = $.parsejson(data.d);
if (typeof callback === "function") {
callback(data);
}
if (isloading == true) { layer.closeall("loading"); }
},
error: function (err) {
//console.log(err);
},
complete: function (xmlhttprequest, textstatus) {
if (isloading == true) { layer.closeall("loading"); }
}
});
},
/*同步执行*/
ajaxsync: function (ajaxurl, postdata, isloading) {
if (ajaxurl.indexof("?") == -1) { ajaxurl = ajaxurl + "?_=" + math.random(); }
var returnjson = '';
$.ajax({
type: 'post',
cache: false,
async: false,//false代表只有在等待ajax执行完毕后才执行下面语句
url: ajaxurl,
data: postdata,
datatype: 'json',
beforesend: function (xmlhttprequest) {
if (isloading == true) { layer.load(2); }
},
success: function (data) {
returnjson = data;
},
complete: function (xmlhttprequest, textstatus) {
if (isloading == true) { layer.closeall("loading"); }
}
});
return returnjson;
},
/*异步执行*/
ajaxasync: function (ajaxurl, postdata, callback, isloading) {
if (ajaxurl.indexof("?") == -1) { ajaxurl = ajaxurl + "?_=" + math.random(); }
$.ajax({
type: 'post',
cache: false,
async: true,//false代表只有在等待ajax执行完毕后才执行下面语句
url: ajaxurl,
data: postdata,
datatype: 'json',
beforesend: function (xmlhttprequest) {
if (isloading == true) { layer.load(2); }
},
success: function (data) {
if (typeof callback === "function") {
callback(data);
}
},
complete: function (xmlhttprequest, textstatus) {
if (isloading == true) { layer.closeall("loading"); }
}
});
},
/*异步加载页面(datatype:html)*/
ajaxpagehtml: function (ajaxurl, callback, isloading) {
if (ajaxurl.indexof("?") == -1) { ajaxurl = ajaxurl + "?_=" + math.random(); }
$.ajax({
type: 'post',
cache: false,
async: true,//false代表只有在等待ajax执行完毕后才执行下面语句
url: ajaxurl,
//data: null,
datatype: 'html',
beforesend: function (xmlhttprequest) {
if (isloading == true) { layer.load(2); }
},
success: function (data) {
if (typeof callback === "function") {
callback(data);
}
},
complete: function (xmlhttprequest, textstatus) {
if (isloading == true) { layer.closeall("loading"); }
}
});
},
/*表单验证*/
validator: function (obj) {
//表单验证
var isvalid = true;
obj = typeof obj == "string" ? document.getelementbyid(obj) : obj;
var elementobjs = $(obj).find("*[data-valid='true']");
elementobjs.each(function () {
var elobj = $(this);
//必填项(data-valid="true" data-required="true" data-msg-required="必填项")
if (elobj.data("required") != null || elobj.data("required") == true) {
if (elobj.val() == null || elobj.val().length == 0) {
var errmsg = "该项为必填项";
if (elobj.data("msg-required") != null || elobj.data("msg-required").length > 0) {
errmsg = elobj.data("msg-required");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
//字符长度(data-length="true" data-length-rule="[n]" data-msg-length="长度限制条件")
if (elobj.data("length") != null || elobj.data("length") == true) {
var tvalue = elobj.val();
if (tvalue != null && tvalue.length > 0) {
var rulevalue = elobj.data("length-rule");
var rulearray = [];
if (jquery.isarray(rulevalue)) {
rulearray = rulevalue;
} else {
rulearray = rulevalue.replace("[", "").replace("]", "").split(",");
}
if ($.inarray("true", rulearray) != -1) {
//console.log("rulevalue=[n~,true],请至少填写 n 个字符(全角字符计算双字符)");
var n1 = rulearray[0].replace("~", "");
if (lengthvalid.getlength(tvalue) < n1) {
var errmsg = "请至少填写 " + n1 + " 个字符";
var datamsg = elobj.data("msg-length");
if ((datamsg != null || datamsg != undefined) && datamsg.length > 0) {
errmsg = datamsg;
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
else if (lengthvalid.ruletest4(rulearray[0])) {
//console.log("rulevalue=[n1~n2],请填写 n1 到 n2 个字符");
var narray = rulearray[0].split("~");
var n1 = narray[0]; n2 = narray[1];
if (lengthvalid.getlength(tvalue) < n1 || lengthvalid.getlength(tvalue) > n2) {
var errmsg = "请录入" + n1 + "到" + n2 + "个字符.";
var datamsg = elobj.data("msg-length");
if ((datamsg != null || datamsg != undefined) && datamsg.length > 0) {
errmsg = datamsg;
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
} else if (lengthvalid.ruletest3(rulearray[0])) {
//console.log("rulevalue=[~n],请最多填写 n 个字符");
var n1 = rulearray[0].replace("~", "");
if (lengthvalid.getlength(tvalue) > n1) {
var errmsg = "请最多填写 " + n1 + " 个字符";
var datamsg = elobj.data("msg-length");
if ((datamsg != null || datamsg != undefined) && datamsg.length > 0) {
errmsg = datamsg;
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
} else if (lengthvalid.ruletest2(rulearray[0])) {
//console.log("rulevalue=[n~],请至少填写 n 个字符");
var n1 = rulearray[0].replace("~", "");
if (lengthvalid.getlength(tvalue) < n1) {
var errmsg = "请至少填写 " + n1 + " 个字符";
var datamsg = elobj.data("msg-length");
if ((datamsg != null || datamsg != undefined) && datamsg.length > 0) {
errmsg = datamsg;
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
} else if (lengthvalid.ruletest1(rulearray[0])) {
//console.log("rulevalue=[n],请填写 n 个字符");
var n1 = rulearray[0];
if (lengthvalid.getlength(tvalue) != n1) {
var errmsg = "请填写 " + n1 + " 个字符";
var datamsg = elobj.data("msg-length");
if ((datamsg != null || datamsg != undefined) && datamsg.length > 0) {
errmsg = datamsg;
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
}
//两值比对(data-compare="agentpassword" data-msg-compare="两次密码输入不一致")
if (elobj.data("compare") != null) {
if (elobj.val().length > 0) {
var tocompareobj = $("#" + elobj.data("compare"));
if (tocompareobj.val() != elobj.val()) {
var errmsg = "两个值不一致";
if (elobj.data("msg-compare") != null || elobj.data("msg-compare").length > 0) {
errmsg = elobj.data("msg-compare");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//邮箱格式验证(data-email="true" data-msg-email="email格式有误")
if (elobj.data("email") != null || elobj.data("email") == true) {
if (elobj.val().length > 0) {
var pattern = /^([\.a-za-z0-9_-])+@([a-za-z0-9_-])+(\.[a-za-z0-9_-])+/;
if (!pattern.test(elobj.val())) {
var errmsg = "邮件格式有误";
if (elobj.data("msg-email") != null || elobj.data("msg-email").length > 0) {
errmsg = elobj.data("msg-email");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//全角半角验证(data-issbccase="true" data-msg-issbccase="存在全角,请在半角下录入。")
if (elobj.data("issbccase") != null || elobj.data("issbccase") == true) {
if (elobj.val().length > 0) {
if (issbccase(elobj.val())) {
var errmsg = "存在全角,请在半角下录入。";
if (elobj.data("msg-issbccase") != null || elobj.data("msg-issbccase").length > 0) {
errmsg = elobj.data("msg-issbccase");
};
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//手机格式验证(data-mobile="true" data-msg-mobile="手机格式有误.")
if (elobj.data("mobile") != null || elobj.data("mobile") == true) {
if (elobj.val().length > 0) {
var pattern = /^1[3-9]\d{9}$/;
if (!pattern.test(elobj.val())) {
var errmsg = "手机格式有误";
if (elobj.data("msg-mobile") != null || elobj.data("msg-mobile").length > 0) {
errmsg = elobj.data("msg-mobile");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-lettersonly="true" data-msg-lettersonly="该项必须为字母"
//仅字母验证
if (elobj.data("lettersonly") != null || elobj.data("lettersonly") == true) {
if (elobj.val().length > 0) {
var pattern = /^[a-za-z]+$/;
if (!pattern.test(elobj.val())) {
var errmsg = "该项必需为字母";
if (elobj.data("msg-lettersonly") != null || elobj.data("msg-lettersonly").length > 0) {
errmsg = elobj.data("msg-lettersonly");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-digits="true" data-msg-digits="该项为整数"
//整数
if (elobj.data("digits") != null || elobj.data("digits") == true) {
if (elobj.val().length > 0) {
var pattern = /^\d+$/;
if (!pattern.test(elobj.val())) {
var errmsg = "该项必需为整数";
if (elobj.data("msg-digits") != null || elobj.data("msg-digits").length > 0) {
errmsg = elobj.data("msg-digits");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-decimal="true" data-msg-decimal="该项为符点数"
//符点数
if (elobj.data("decimal") != null || elobj.data("decimal") == true) {
if (elobj.val().length > 0) {
var pattern = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
if (!pattern.test(elobj.val())) {
var errmsg = "该项必需为符点数";
if (elobj.data("msg-decimal") != null || elobj.data("msg-decimal").length > 0) {
errmsg = elobj.data("msg-decimal");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//字母和数字的验证(data-chrnum="true" data-msg-chrnum="该项必须为字母或数字")
if (elobj.data("chrnum") != null || elobj.data("chrnum") == true) {
if (elobj.val().length > 0) {
var pattern = /^([a-za-z0-9]+)$/;
if (!pattern.test(elobj.val())) {
var errmsg = "该项必需为字母或数字";
if (elobj.data("msg-chrnum") != null || elobj.data("msg-chrnum").length > 0) {
errmsg = elobj.data("msg-chrnum");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-alphanumeric="true" data-msg-alphanumeric="该项必须为字母或数字或下划线"
//字母、数字、下划线组成
if (elobj.data("alphanumeric") != null || elobj.data("alphanumeric") == true) {
if (elobj.val().length > 0) {
var pattern = /^\w+$/;
if (!pattern.test(elobj.val())) {
var errmsg = "该项必需为字母或数字或下划线组成";
if (elobj.data("msg-alphanumeric") != null || elobj.data("msg-alphanumeric").length > 0) {
errmsg = elobj.data("msg-alphanumeric");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-zipcode="true" data-msg-zipcode="邮编格式验证"
//中文验证
if (elobj.data("zipcode") != null || elobj.data("zipcode") == true) {
if (elobj.val().length > 0) {
var pattern = /^[0-9 a-za-z]{5,20}$/;
if (!pattern.test(elobj.val())) {
var errmsg = "邮编格式有误";
if (elobj.data("msg-zipcode") != null || elobj.data("msg-zipcode").length > 0) {
errmsg = elobj.data("msg-zipcode");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-minlength="6" data-msg-minlength="最小长度验证"
//最小长度验证
if (elobj.data("minlength") != null || parseint(elobj.data("minlength")) > 0) {
if (elobj.val().length > 0) {
var minlength = parseint(elobj.data("minlength"));
if (elobj.val().length < minlength) {
var errmsg = "该项最小长度为 [minlength].";
if (elobj.data("msg-minlength") != null || elobj.data("msg-minlength").length > 0) {
errmsg = elobj.data("msg-minlength");
}
errmsg = errmsg.replace("[length]", minlength);
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-maxlength="20" data-msg-maxlength="最大长度验证"
//最大长度难证
if (elobj.data("maxlength") != null || parseint(elobj.data("maxlength")) > 0) {
if (elobj.val().length > 0) {
var maxlength = parseint(elobj.data("maxlength"));
if (elobj.val().length > maxlength) {
var errmsg = "该项最大长度为 [length].";
if (elobj.data("msg-maxlength") != null || elobj.data("msg-maxlength").length > 0) {
errmsg = elobj.data("msg-maxlength");
}
errmsg = errmsg.replace("[length]", maxlength);
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-idcard="true" data-msg-idcard="身份证格式验证"
//身份证格式
if (elobj.data("idcard") != null || parseint(elobj.data("idcard")) > 0) {
if (elobj.val().length > 0) {
var jsonidcard = idcardvalid(elobj.val());
if (jsonidcard.isvalid == false) {
var errmsg = "身份证格式有误";
if (elobj.data("msg-idcard") != null || elobj.data("msg-idcard").length > 0) {
errmsg = elobj.data("msg-idcard");
}
if (jsonidcard.tip.length > 0) { errmsg = jsonidcard.tip; }
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
//data-custom="customvalid" data-msg-custom="自定义验证";
if (elobj.data("custom") != null || (elobj.data("custom") != undefined && elobj.data("custom").length > 0)) {
var funname = elobj.data("custom");
var customfunctin = eval(funname);
if (customfunctin(this) == false) {
var errmsg = "自定义验证.";
if (elobj.data("msg-custom") != null || elobj.data("msg-custom").length > 0) {
errmsg = elobj.data("msg-custom");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
//中文验证(data-chinese="true" data-msg-chinese="该项必须为中文")
if (elobj.data("chinese") != null || elobj.data("chinese") == true) {
if (elobj.val().length > 0) {
var pattern = /^[\u4e00-\u9fa5]+$/;
if (!pattern.test(elobj.val())) {
var errmsg = "只能输入中文";
if (elobj.data("msg-chinese") != null || elobj.data("msg-chinese").length > 0) {
errmsg = elobj.data("msg-chinese");
}
promptmessage(elobj, errmsg);
elobj.focus();
isvalid = false;
return false;
}
}
}
});
return isvalid;
},
/*序列化表单-生成json数据*/
serialize: function (obj) {
//将表单转换成json格式数据
var postdata = {};
obj = typeof obj == "string" ? document.getelementbyid(obj) : obj;
//var elementobjs = $("#" + formid + " *[data-field-name]");
var elementobjs = $(obj).find("*[data-field-name]");
elementobjs.each(function () {
var elobj = $(this);
var name = elobj.data("field-name");
var type = elobj.attr("type");
var tagname = elobj[0].tagname;
if (tagname != undefined && tagname == "p") {
type = elobj.data("type");
if (type == "radiogroup".tolowercase()) {
//单选按钮组
var radiovalue = elobj.find("input[type='radio']:checked").val();
if (radiovalue != undefined) {
postdata['' + name + ''] = '' + radiovalue + '';
} else {
postdata['' + name + ''] = '';
}
} else if (type == "checkboxgroup".tolowercase()) {
//复选框组
var checkboxobjs = elobj.find("input[type='checkbox']:checked");
var ckvalues = [];
checkboxobjs.each(function () {
if (this.checked == true) {
ckvalues.push($(this).val());
}
});
if (ckvalues.length > 0) {
postdata['' + name + ''] = '' + ckvalues.tostring() + '';
} else {
postdata['' + name + ''] = '';
}
}
} else {
switch (type) {
case "checkbox":
{
//data-field-name="name" data-datatype="int"
//data-field-name=文件名
//data-datatype=数据类型(int-返回1或0,bool返回true或false,string返回value值)
if (elobj.is(":checked") == true) {
if (elobj.data("datatype") == "bool") {
postdata['' + name + ''] = "true";
} else {
postdata['' + name + ''] = "1";
}
} else {
if (elobj.data("datatype") == "bool") {
postdata['' + name + ''] = "false";
} else {
postdata['' + name + ''] = "0";
}
}
}
break;
default:
{
var value = elobj.val();
postdata['' + name + ''] = value;
}
break;
}
}
});
return postdata;
},
/*
* 表单赋初始始值
*/
setvalue: function (fieldname, fieldvalue) {
if (fieldname == undefined) { return false; }
var elobj = $("*[data-field-name='" + fieldname + "']");
if (elobj != undefined) {
var tagname = elobj[0].tagname;
if (tagname != undefined && tagname == "p") {
var type = elobj.data("type");
if (type == "checkboxgroup".tolowercase()) {
//复选框组
if (fieldvalue == undefined) {
var hidvalue = elobj.find("input[type='hidden']").val();
if (hidvalue != undefined) { fieldvalue = hidvalue; }
}
if (fieldvalue != undefined && fieldvalue.length != 0) {
var ckobjs = elobj.find("input[type='checkbox']");
ckobjs.each(function () { this.checked = false; })
var valuearray = fieldvalue.split(',');
for (var i = 0, intcount = valuearray.length; i < intcount; i++) {
ckobjs.each(function () {
var ckvalue = $(this).val();
if (ckvalue == valuearray[i]) {
this.checked = true;
}
});
}
}
} else if (type == "radiogroup".tolowercase()) {
//单选框组
if (fieldvalue == undefined) {
var hidvalue = elobj.find("input[type='hidden']").val();
if (hidvalue != undefined) { fieldvalue = hidvalue; }
if (fieldvalue != undefined) { fieldvalue = hidvalue; }
if (fieldvalue != undefined && fieldvalue.length != 0) {
var radioobjs = elobj.find("input[type='radio']");
radioobjs.each(function () { this.checked = false; })
var valuearray = fieldvalue.split(',');
for (var i = 0, intcount = valuearray.length; i < intcount; i++) {
radioobjs.each(function () {
var radiovalue = $(this).val();
if (radiovalue == valuearray[i]) {
this.checked = true;
}
});
}
var checkedobj = elobj.find("input[type='radio']:checked");
if (checkedobj.length == 0) { radioobjs.eq(0)[0].checked = true; }
}
}
}
} else {
if (fieldvalue != undefined) {
elobj.val(fieldvalue);
}
}
}
},
/*将json数据转化成字符串*/
stringify: function (json) {
//将json转换成字符串参数
var postdata = {};
if (json != undefined) {
postdata = { "paramsdata": json.stringify(json) };
}
return postdata;
}
};
//提示信息
function promptmessage(element, messagetext) {
var isdialog = "false";
if ($(element).data("dialog") != null) {
isdialog = $(element).data("dialog").tostring();
}
if (isdialog == "true") {
layer.msg(messagetext);
} else {
layer.tips(messagetext, $(element), { tips: [1, '#3595cc'], time: 3000 });
}
}
; var lengthvalid = {
ruletest1: function (value) {
//rulevalue=[n],请填写 n 个字符
var pattern = /^\d{1,}$/;
return pattern.test(value);
},
ruletest2: function (value) {
//rulevalue=[n~],请至少填写 n 个字符
var pattern = /^\d{1,}~$/;
return pattern.test(value);
},
ruletest3: function (value) {
//rulevalue=[~n],请最多填写 n 个字符
var pattern = /^~\d{1,}$/;
return pattern.test(value);
},
ruletest4: function (value) {
//rulevalue=[n1~n2],请填写 n1 到 n2 个字符
var pattern = /^\d{1,}~\d{1,}$/;
return pattern.test(value);
},
getlength: function (str) {
///获得字符串实际长度,中文2,英文1
///要获得长度的字符串
var reallength = 0, len = str.length, charcode = -1;
for (var i = 0; i < len; i++) {
charcode = str.charcodeat(i);
if (charcode >= 0 && charcode <= 128)
reallength += 1;
else
reallength += 2;
}
return reallength;
}
}
/*
* 身份证号合法性验证
* 支持15位和18位身份证号
* 支持地址编码、出生日期、校验位验证
*/
function idcardvalid(code) {
var jsonoutput = {};
jsonoutput.isvalid = true;
jsonoutput.tip = "";
if ((code.length != 15) && (code.length != 18)) {
jsonoutput.tip = "身份证长度不对";
jsonoutput.isvalid = false;
}
var zcode = code.match(/[\uff00-\uffff]/g);
if (zcode != null) {
jsonoutput.tip = "身份证存在全角字符";
jsonoutput.isvalid = false;
}
try {
var validator = new idvalidator();
var jsonidcard = validator.isvalid(code);
if (jsonidcard == false) {
jsonoutput.tip = "身份证格式有误.";
jsonoutput.isvalid = false;
};
} catch (e) { }
return jsonoutput;
}
/*
* 名称:issbccase(全角半角校验)
* 功能:判断字符串中是否存在全角字符串
* 参数:source,源字符串;
* 返回:是否存在全角字符,true或false
*/
function issbccase(strtmp) {
for (var i = 0; i < strtmp.length; i++) {
if (strtmp.charcodeat(i) > 128) {
return true;
break;
}
}
return false;
}