/**
* js校验form表单
*
* liqingqing 2014-07-09
*
*
* 校验参数
* nocheck=false: 不校验数据
* notnull=true : 非空
* isint : 整形数字校验
* isfloat : 浮点数字校验
* dotlength : 小数点后面的长度
* maxlength : 文本最大长度,好像有点多余:(
* minlength : 文本最小长度
* maxvalue : 文本最大值,数字比较
* minvalue : 文本最小值,数字比较
* isdate : 日期格式(19位或10位)
* islongdate : 长日期格式(19位)
* isshortdate : 短日期格式(10位)
* maxdate : 最大日期
* mindate : 最小日期
* passwordkey : 密码相同校验
* isemail : 邮箱格式校验
* isidcard : 身份证格式校验
* ismumber : 只能是数字
* ischar : 只能是字母
* isalpha : 只能是数字、下划线、字母
* ischinese : 只能是汉字
* jskeyword : 特殊字符校验(单引号、双引号、左右尖括号)
* sqlkeyword : sql关键字校验(and、or、in、select、update、delete、create、drop)
* maxitem : 复选框最多选择项数
* minitem : 复选框最少选择项数
* allow='jpg|gif|png' : 允许上传的文件格式 用于【input type=file】
* notallow="exe|msi|java" : 禁止上传的文件格式 用于【input type=file】
*
* 其他参数
* lable : 字段名称(必需,用于消息提示)
*
* 预留接口
* 1 beforesubmit_form1(); //在所有控件开始校验前
* 2 aftersubmit_form1(); //在所有控件开始校验成功后
* 3 beforecheckelement_form1(element); //在校验每一个需要校验的控件前
* 4 aftercheckelement_form1(element); //在校验每一个需要校验的控件后
* 5 checkfailure_form1(element,type,msg); //字段校验失败后触发函数
*
* form1 为表单的name
* element 为当前校验对象
* type 校验失败类型
* msg 错误信息
*
*
* 注意:
* 1:校验参数写在表单对象的属性中 例如
* 2:在需要校验的表单对象的属性中写qcheck="true"
*
*/
function qingsoftcheck(formelement){
this.uid = qingsoftcheckhelp.getid();
this._form = formelement;
qingsoftcheckhelp.instance[this.uid] = this;
var self = this;
}
qingsoftcheck.prototype={
submitcheck: function(){
var okname = new map();
var elemens = this._form.getelementsbytagname('*');
//var formname = this._form.getattribute("name");
var formname =this._form.attributes['name'].value;
for(var i=0;i0 || element.minitem>0){
var s = this.maxminitem(element);
if(s !=""){
this.checkfailure(element,"maxminitem",lable+s);
return false;
}
}
return true;
},
checkradio: function(element){
var lable = element.lable;
//非空校验
if(element.notnull){
var v = qutil.getfromelementsbyname(this._form,element.name);
for (var i = 0; i < v.length; i++) {
if (v[i].checked) {return true;}
}
this.checkfailure(element,"notnull",lable+"必需选择");
return false;
}
return true;
},
checkfile: function(element){
var value =qutil.trim(element.value);
var lable = element.lable;
//非空校验
if(element.notnull && qutil.isnull(value)){
this.checkfailure(element,"notnull",lable+"必需指定一个文件");
return false;
}
if(element.allow!="" && value!="" && value.lastindexof(".")){
var d = value.substring(value.lastindexof(".")+1).tolowercase();
var ks = element.allow.tolowercase().split("|");
if(d!="" && ks.length>0){
for(var i=0;i0){
for(var i=0;i0 || element.minlength>0){
var s = this.maxminlength(element);
if(s !=""){
this.checkfailure(element,"maxminlength",lable+s);
return false;
}
}
//整形校验
if(element.isint && !qutil.isint(value)){
this.checkfailure(element,"isint",lable+"必需是整数");
return false;
}
//浮点数校验
if(element.isfloat && !qutil.isnumeric(value)){
this.checkfailure(element,"isfloat",lable+"必需是浮点数");
return false;
}
//小数点长度
if(element.dotlength>0){
var temp = value.length - value.lastindexof(".");
if(isnan(temp) || temp-1 != element.dotlength){
this.checkfailure(element,"dotlength",lable+"小数位长度必需是"+element.dotlength+"位");
return false;
}
}
//纯数字校验
if(element.ismumber && !qutil.ismumber(value)){
this.checkfailure(element,"ismumber",lable+"只能填写数字");
return false;
}
//纯字母校验
if(element.ischar && !qutil.ischar(value)){
this.checkfailure(element,"ischar",lable+"只能填写字母");
return false;
}
//纯汉字校验
if(element.ischinese && !qutil.ischinese(value)){
this.checkfailure(element,"ischinese",lable+"只能填写中文字符");
return false;
}
//只能是字母数字和下划线
if(element.isalpha && !qutil.isalpha(value)){
this.checkfailure(element,"isalpha",lable+"只能是字母、数字或下划线");
return false;
}
//最大值或最小值
if(element.maxvalue !=null || element.minvalue !=null){
var s = this.maxminvalue(element);
if(s !=""){
this.checkfailure(element,"maxminvalue",lable+s);
return false;
}
}
//日期格式校验
if(element.isdate && !qutil.isdate(value)){
this.checkfailure(element,"isint",lable+"无效的日期格式(格式请参照:yyyy-mm-dd 或 yyyy-mm-dd hh:mm:ss )");
return false;
}
//长日期格式校验
if(element.islongdate && !qutil.islongdate(value)){
this.checkfailure(element,"isint",lable+"无效的日期格式(格式请参照:yyyy-mm-dd hh:mm:ss )");
return false;
}
//短日期格式校验
if(element.isshortdate && !qutil.isshortdate(value)){
this.checkfailure(element,"isint",lable+"无效的日期格式(格式请参照:yyyy-mm-dd)");
return false;
}
//最大日期和最小日期校验
if(element.maxdate!="" || element.mindate!=""){
var s = this.maxmindate(element);
if(s !=""){
this.checkfailure(element,"maxmindate",lable+s);
return false;
}
}
//相同密码
if(element.passwordkey!="" && !this.checkpwds(element)){
this.checkfailure(element,"passwordkey",lable+"输入不一致");
return false;
}
//邮箱格式
if(element.isemail!="" && !qutil.isemail(element.value)){
this.checkfailure(element,"isemail",lable+"不是一个有效的邮箱地址格式");
return false;
}
//身份证格式
if(element.isidcard!="" && !qutil.isidcard(element.value)){
this.checkfailure(element,"isemail",lable+"不是一个有效的身份证格式");
return false;
}
return true;
},
//校验多个输入框的值是否相同
checkpwds: function (element) {
var key = element.passwordkey;
var elemens = this._form.getelementsbytagname('input');
for(var i=0;i0){return "只能是"+max+"之前";}
if(min!="" && qutil.datediff(_min,value,"s")<0){return "只能是"+min+"之后";}
return "";
},
maxminlength: function (element) {
var max = element.maxlength;
var min = element.minlength;
var len = qutil.trim(element.value).length;
if(max>0 && (max == min) && max!=len){
return "长度必需等于"+max;
}
if(max>0 && len>max){
return "长度必需小于或等于"+max;
}
if(min>0 && lenqutil.parsefloat(max)){
return "值必需小于或等于"+max;
}
if(min!=null && value0 && (max == min) && max!=ccount){
return ("必需选择"+ccount+"项");
}
if(max>0 && ccount>max){
return ("只能选择"+max+"项");
}
if(min>0 && ccount= value.length){
return ((this.trim(value)).replace(/[.\/]/g,'-').search(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/) > -1);
}else{
return ((this.trim(value)).replace(/[.\/]/g,'-').search(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\d):[0-5]?\d((:[0-5]?\d$)|($))/) > -1);
}
},
isdate: function(value){
return this.isalldate(value,1)
},
islongdate: function(value){
return this.isalldate(value,2)
},
isshortdate: function(value){
return this.isalldate(value,3)
},
isstring: function(object){
return ('string' == typeof(object));
},
isemail: function(value){
return (value.search(/^([a-za-z0-9]+[_|_|.]?)*[a-za-z0-9]+@([a-za-z0-9]+[_|_|.]?)*[a-za-z0-9]+\.(?:com|cn)$/)!= -1);
},
ismumber: function(value){
return (/^[0-9]*$/.test(value));
},
ischar: function(value){
return (/^[a-za-z]*$/.test(value));
},
ischinese: function(value){
return (/^[\u4e00-\u9fa5]*$/.test(value));
},
isalpha: function(value){
return (value.replace(/\w/g, "").length == 0);
},
isidcard: function(value){
var wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];
var validecode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];
function idcardvalidate(idcard){
idcard = trim(idcard.replace(/ /g, ""));
if (idcard.length == 15) {
return isvaliditybrithby15idcard(idcard);
} else if (idcard.length == 18) {
var a_idcard = idcard.split("");
return (isvaliditybrithby18idcard(idcard)&&istruevalidatecodeby18idcard(a_idcard));
} else {
return false;
}
}
function istruevalidatecodeby18idcard(a_idcard) {
var sum = 0;
if (a_idcard[17].tolowercase() == 'x') {a_idcard[17] = 10;}
for ( var i = 0; i < 17; i++) {sum += wi[i] * a_idcard[i];}
valcodeposition = sum % 11;
return (a_idcard[17] == validecode[valcodeposition]);
}
function isvaliditybrithby18idcard(idcard18){
var year = idcard18.substring(6,10);
var month = idcard18.substring(10,12);
var day = idcard18.substring(12,14);
var temp_date = new date(year,parsefloat(month)-1,parsefloat(day));
return !(temp_date.getfullyear()!=parsefloat(year) || temp_date.getmonth()!=parsefloat(month)-1 || temp_date.getdate()!=parsefloat(day));
}
function isvaliditybrithby15idcard(idcard15){
var year = idcard15.substring(6,8);
var month = idcard15.substring(8,10);
var day = idcard15.substring(10,12);
var temp_date = new date(year,parsefloat(month)-1,parsefloat(day));
return !(temp_date.getyear()!=parsefloat(year) ||temp_date.getmonth()!=parsefloat(month)-1 ||temp_date.getdate()!=parsefloat(day));
}
function trim(str){ return str.replace(/(^\s*)|(\s*$)/g, ""); }
return idcardvalidate(value);
},
jskeyword:function(value){
return !(value.indexof("'")>=0 || value.indexof('"')>=0 || value.indexof('<')>=0|| value.indexof('>')>=0 );
},
sqlkeyword:function(value){
value = value.tolowercase();
return !(value.indexof("and")>=0 || value.indexof('or')>=0 || value.indexof('select')>=0|| value.indexof('update')>=0|| value.indexof('delete')>=0|| value.indexof('create')>=0|| value.indexof('drop')>=0 );
},
trim: function(value){
return value.replace(/(^\s*)|(\s*$)/g, "");
},
getattr: function (element,attrname) {
var obj = element.getattribute(attrname);
if(obj == null){return "";}
return obj.tostring();
},
getelement: function(obj){
if(typeof(obj)=="string" && ""!=obj){return document.getelementbyid(obj)}
else return obj;
},
datediff: function(cd1, cd2, ntype){
var cd1 = (this.isstring(cd1)) ? this.dateformat(cd1, '/') : '';
var cd2 =(this.isstring(cd2)) ? this.dateformat(cd2, '/') : '';
if ('' == cd1 || '' == cd2){ return 0; }
var ntype = (this.isstring(ntype)) ? ntype.tolowercase() : '';
var clen = 10;
if ('h' == ntype){ clen = 14; }
else if ('i' == ntype){ clen = 16; }
else if ('s' == ntype){ clen = 19; }
var d1 = new date(cd1.substr(0, clen));
var d2 = new date(cd2.substr(0, clen));
var longs = d2.gettime() - d1.gettime();
switch(ntype){
case 'y': return this.parseint((d2.getfullyear() - d1.getfullyear()));
case 'm': return this.parseint(((d2.getfullyear()-d1.getfullyear())*12+(d2.getmonth()-d1.getmonth())));
case 'h': return this.parseint(longs/3600000);
case 'i': return this.parseint(longs/60000);
case 's': return this.parseint(longs/1000);
default: return this.parseint(longs/86400000);
}
},
dateformat: function(odate, listsep){
var odate = (this.isstring(odate) && this.isdate(odate)) ? new date(odate.replace(/[-.]/g, '/')) : odate;
var listsep = ('/' == this.trim(listsep)) ? '/' : '-';
if (isnan(odate) || 'function' != typeof(odate.getfullyear)){ return ''; }
tmpstr = odate.getfullyear();
var month = odate.getmonth() + 1;
var day = odate.getdate();
tmpstr += listsep + (month < 10 ? '0' : '') + month + listsep + (day < 10 ? '0' : '') + day;
var hour = odate.gethours();
var minute = odate.getminutes();
var second = odate.getseconds();
if (0 < hour || 0 < minute || 0 < second)
{
tmpstr += ' ' + (10 > hour ? '0' : '') + hour + ':' + (10 > minute ? '0' : '') + minute
+ ':' + (10 > second ? '0' : '') + second;
}
return tmpstr;
},
getfromelementsbyname: function(form, name){
var arr = [];
if(form!=null){
var elemens = form.getelementsbytagname('*');
for(var i=0;i