/*** *验证组件,通过valid属性进行标识,通过validname和validlname内容进行提示。 * * @param flag {是否验证隐藏域,默认不验证,true验证} todo 待议 * *@return boolean {验证成功返回true 验证失败返回false,并提示失败信息} */ function validform(flag){ var visible = ''; flag = flag || false; if(flag) visible = ':visible'; var having=true; var notice ; $("[valid^='v']"+visible).each(function() { //提示信息,如果validlname存在直接输出,如果不存在通过validname拼接默认提示 var that = $(this); var valid = that.attr("valid"); var valids = valid.split("_"); if(isempty(that.val())){ //处理必填项(v*_y) if(valids[1]=='y') { _valid("不能为空",""); } //处理必填项(vtext) if(valids[0]=='vtext') { _valid("不能为空",""); } //处理时间组件不能为空,特殊处理 else if(valids[0]=='vdate'){ if(isempty(that.val())) _valid("不能为空",""); } }else{ //处理下拉选择框 if(valids[0]=='vselect') { if(that.val()=='-1') _valid("必须选择","-1"); } //处理整数类型(vnum_n_0_31) else if(valids[0]=='vnum') { if(!isint(that.val())) _valid("必须为整数",""); //数字区间验证 else if(!checkint(that.val(),valids[2],valids[3])) _valid("必须在"+valids[2]+"和"+valids[3]+"之间",""); } //处理数字类型 else if(valids[0]=='vfloat') { if(!isfloat(that.val())) _valid("必须为数字",""); //数字区间验证 else if(!checkfloat(that.val(),valids[2],valids[3])) _valid("必须在"+valids[2]+"和"+valids[3]+"之间",""); } //处理email else if(valids[0]=='vemail') { if(!isemail(that.val())) _valid("格式错误",""); } //处理手机号码 else if(valids[0]=='vmobile') { if(!ismobile(that.val())) _valid("格式错误",""); } //处理电话号码 else if(valids[0]=='vphone') { if(!isphone(that.val())) _valid("格式错误",""); } } function _valid(noticevalue,defaultvalue){ var content = that.attr('validname') || ''; var lcontent = that.attr('validlname'); notice = typeof(lcontent) == 'undefined'?content + noticevalue + ',请重新填写!':lcontent; if(having) alert(notice,function(){ if(valids[0]!='vdate') that.focus(); if(valids[0]=='vdate') that[0].focus(); }); if(valids[0]!='vdate'){ //vdate不做样式处理 that.css("border","1px dashed red"); that.bind("change",function(){ var solid = "1px solid #a5ccf8"; if(valids[0]=='vnum'){ if(checkint(that.val(),valids[2],valids[3])) that.css("border",solid); } else if(valids[0]=='vfloat'){ if(checkfloat(that.val(),valids[2],valids[3])) that.css("border",solid); } else if(valids[0]=='vemail') { if(isemail(that.val())) that.css("border",solid); } else if(valids[0]=='vmobile') { if(ismobile(that.val())) that.css("border",solid); } else if(valids[0]=='vphone') { if(isphone(that.val())) that.css("border",solid); } else { if(that.val() != defaultvalue) that.css("border",solid); }; }); } having=false; } }); return having; } /** * 验证成功后,提交表单 * * @param url {提交的url} * @param formobj {form对象} * @param flag {是否验证隐藏域,默认不验证,true验证} * * @return {boolean} */ function submitform(url,formobj,flag){ if(validform(flag)){ url = url || ''; formobj = formobj || document.form1; if(url != '') formobj.action=url; formobj.submit(); } } /** * 判断某个字符是否为空,如果为空则返回true,否则返回false. * * @param {我们要验证的字符} * str * @return {boolean} */ function isempty(str) { str = str || ''; if ((str == null) || (str.length == 0)) return true; else return false; } /** * 判断某个字符是否是数字. * * @param {我们要验证的字符} * thenum * @return {boolean} */ function isdigit(thenum) { var themask = '0123456789'; if (isempty(thenum)) return false; else if (themask.indexof(thenum) == -1) return false; return true; } /** * 判断某个字符是不是整型,第二个参数是是否支持负数,默认不支持负数. * * @param {我们要验证的字符} * thestr * @param {判断是不是支持负数,默认不支持负数} * isnegative * @return {} 使用示例: var age = form1.age.value; if(!isint(age,true)){ * alert("年龄必须为整数!"); return ; } */ function isint(thestr, isnegative) { isnegative = isnegative || false; var flag = true; if (isempty(thestr)) { flag = false; } else { var i = thestr.length ; while (i--) { if (isnegative == true && i == 0) { if (thestr.substring(0, 1) == '-' && thestr.length == 1) { flag = false; break; } else if (thestr.substring(0, 1) != '-' && isdigit(thestr.substring(0, 1)) == false) { flag = false; break; } } else if (isdigit(thestr.substring(i, i + 1)) == false) { flag = false; break; } } } return flag; } /******************************************** 检测数字区间 thestr-被检测字符串 min-最小值(为''时不进行此项检测) max-最大值(为''时不进行此项检测) ********************************************/ function checkint ( thestr,min, max ) { min = min || ''; max = max || ''; if (!isint(thestr)) { return false; }else{ if ( !isempty(min) && parseint( thestr ) < min ) { return false; } if ( !isempty(max) && parseint( thestr ) > max ) { return false; } } return true; } /** * 判断是不是浮点数,第二个参数是指是否支持负数. * * @param {我们要验证的字符} * thestr * @param {是否支持负数} * isnegative * @return {boolean} 使用示例: var money = form1.money.value; * if(!isfloat(money,true)){ alert("试用期工资必须为数字格式!"); return ; } */ function isfloat(thestr, isnegative) { var dot1st = thestr.indexof('.'); var dot2nd = thestr.lastindexof('.'); if (isempty(thestr)) return false; if (dot1st == -1) { if (isint(thestr, isnegative)) return true; else return false; } else if (dot1st != dot2nd || dot1st == 0) return false; else { var intpart = thestr.substring(0, dot1st); var decpart = thestr.substring(dot2nd + 1); // 判断是否支持负数:是指前一部分是否支持负数,后面的部分不支持负数 if (!isint(intpart, isnegative) || !isint(decpart)) return false; else if (isempty(decpart)) return false; else return true; } } /******************************************** 检测是否是数字类型、并且大于最小值,小于最大值 thestr-被检测字符串 min-最小值(为null时不进行此项检测) max-最大值(为null时不进行此项检测) ********************************************/ function checkfloat ( thestr,min, max ) { min = min || ''; max = max || ''; if (!isfloat(thestr)) { return false; }else{ if ( !isempty(min) && parsefloat( thestr ) < min ) { return false; } if ( !isempty(max) && parsefloat( thestr ) > max ) { return false; } } return true; } //邮箱验证方法,如果是空返回false function isemail(thestr) { var pattern = /(^\w+((-\w+)|(\.\w+))*\@[a-za-z0-9]+((\.|-)[a-za-z0-9]+)*\.[a-za-z0-9]+$)/; return _match(thestr,pattern) ; } //手机号码验证信息,如果是空返回false function ismobile(thestr) { var pattern = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/; return _match(thestr,pattern) ; } //判断是否为正确的电话号码(可以含"()"、"()"、"+"、"-"和空格),如果是空返回false function isphone(thestr){ //国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)" var pattern =/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)/; return _match(thestr,pattern) ; } //正则匹配字符串 function _match (thestr,pattern) { if (isempty(thestr)) return false; if(!pattern.exec(thestr)) { return false; } return true; } /** * 检测是否是字母 * @param {} inputval * @return {boolean} */ function isletter( inputval ) { if (isempty(inputval)) return false; inputval = inputval.touppercase(); var strcheck = "abcdefghijklmnopqrstuvwxyz"; var i = inputval.length; while (i--) { var s = inputval.charat( i ); var npos = strcheck.indexof( s ); if ( npos < 0 ) { return false; } } return true; } /** * 判断某个字符串是否为日期格式,指定的日期格式为2011-02-04. * @param {} datestr * @return {boolean} */ function isdate(datestr) { if(isempty(datestr)){ return false; } datestr = datestr.replace(/\s+/g, ""); // var reg = /^[1,2][\d]{3}[\-\/]([0]{0,1}[1-9]|1[1,2])[\-\/][0,1,2,3]{0,1}[\d]$/; var reg = /^[1,2][\d]{3}[\-]([0]{0,1}[1-9]|1[1,2])[\-][0,1,2,3]{0,1}[\d]$/; if (!datestr.match(reg)) { return false; } var ary, d, day; ary = datestr.replace('/', '-').split('-'); d = new date(ary[0], ary[1], 0); day = parseint(ary[2], 10); if (day < 1 || day > d.getdate()) { return false; } return true; } /** * 判断是否是简单的年月,格式为201107. * @param {} datestr * @return {boolean} */ function issimpledate(datestr) { if(isempty(datestr)){ return false; } datestr = datestr.replace(/\s+/g, ""); // var reg = /^[1,2][\d]{3}[\-\/]([0]{0,1}[1-9]|1[1,2])[\-\/][0,1,2,3]{0,1}[\d]$/; var reg = /^[1,2][\d]{3}([0]{0,1}[1-9]|1[0,1,2])$/; if (!datestr.match(reg)) { return false; } return true; } /** * 限制文本域、文本框的字符数量,如果超过指定的数量,那么把进行字符的截取. * * @param field * :我们的文本域控件 * @param maxlimit : * 最大的字符长度 * @param countfield : * 我们用于显示还可以输入多少个字符的文本框控件,该参数可以省略 使用示例:
* 您还可以输入:个字符
*/ function textcounter(field, maxlimit, countfield) { // 如果文本域的长度大于最大的长度,那么需要截取字符串 if (field.value.length > maxlimit) { // 如果元素区字符数大于最大字符数,按照最大字符数截断; field.value = field.value.substring(0, maxlimit); } else { if ((countfield || '') != '') { // 在记数区文本框内显示剩余的字符数; countfield.value = maxlimit - field.value.length; } } } //表单验证特殊符号 function vaildscript(s) { var pattern = new regexp( "[`~!@#$^&*()=|{}':;'.,\\[\\].<>/?~!% @#¥……&*()——|{}【】‘;:”“'。,、?]") var rs = ""; for ( var i = 0; i < s.length; i++) { var rst = s.substr(i, 1).replace(pattern, ''); if (rst == "") { return "error"; } rs = rs + rst; } return rs; }