//小工具函数 var ccTools = { checkImgSuffix:function(_str){ //是否是合法图像文件 _str = ccTools.getSuffix(_str); return ccTools.isImgSuffix(_str); }, isImgSuffix: function(_str){ //合法的图像后缀 var imgs=["gif","png","jpg","jpeg"]; var x; var txt=""; _str = _str.toLocaleLowerCase(); //小写 for (x in imgs){ if( _str === imgs[x] ){return true;} } return false; }, getSuffix: function(_str){ //文件名后缀 var _idx = _str.lastIndexOf("."); var _str11 = ""; if(_idx===-1){ return ""; }else{ _str11 = _str.substring( (_idx+1),_str.length); return _str11; } }, log: function(){} }; /* 调用示例: var a=ccTools.getSuffix("123456.PNG"); var b=ccTools.isImgSuffix("PNG"); if ( ccTools.checkImgSuffix("123456.PNG") ){alert("是合法的图像文件");} */
var isName = /(^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+[0-9]{8})$)|(^([a-zA-Z0-9_|-|.])+@([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,4}$)/; var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+[0-9]{8})$/; var isEmail = /^([a-zA-Z0-9_|-|.])+@([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,4}$/; var isTel = /^([0-9_.,|-]{7,15})$/; //验证函数 var verifyReg = { data : '', getLenCheck : function(_ipt){ //(1汉字=2字符)。有违规字符,返回-1;合法,返回长度。 var userName = $.trim(_ipt.val()); var en = 0; var zh = 0; var isName = true; var nameStr = /^[u4e00-u9fa5.a-zA-Z0-9]+$/; //只允许字符:(汉字 a-z A-Z 0-9) var patt = new RegExp(nameStr); var length = userName.length; if( length>0 ){ if(!patt.test(userName)){isName = false;}//是否含有违规字符 for(var i=0; i<length; i++){ userName.charCodeAt(i) > 255 ? zh++ : en++; } if( isName===false ){return -1;}//有违规字符,返回-1 } return (zh*2 + en); }, getLen : function(_ipt){ //(1汉字=2字符)。返回长度。不判断违规字符 var str000 = $.trim(_ipt.val()); var en = 0; var zh = 0; var len = str000.length; if( len>0 ){ for(var i=0; i<len; i++){ str000.charCodeAt(i) > 255 ? zh++ : en++; } } return (zh*2 + en); }, substringLen : function(str000,lenLimit){ //(1汉字=2字符)。返回字符串。 var len000 = str000.length; var en = 0; var zh = 0; var sum = 0; if( lenLimit<=0 || lenLimit>=(len000*2) ){return str000;} for(var i=0; i<len000; i++){ str000.charCodeAt(i) > 255 ? zh++ : en++; sum = zh*2 + en; //alert("zh"+(zh*2)+" en:"+en+", sum:"+sum+" ,lenLimit:"+lenLimit+""); if(sum>lenLimit){return str000.substring(0,i);} } return str000; }, verifyName : function(_ipt){ var userName = $.trim(_ipt.val()); var en = 0; var zh = 0; var nameStr = /^[u4e00-u9fa5.a-zA-Z0-9]+$/; //只允许字符:(汉字 a-z A-Z 0-9) var patt = new RegExp(nameStr); if(!patt.test(userName)){return false;}//验证违规字符 for(var i = 0; i < userName.length;i++){ userName.charCodeAt(i) > 255 ? zh++ : en++; } //alert("en:"+en+", zh:"+zh+", other:"+isName); //return (zh+en); return true; }, log: function(){} }; /* 调用示例: var len = verifyReg.getLenCheck( $_ipt ); //(1汉字=2字符)。有违规字符,返回-1;合法,返回长度。 var len = verifyReg.getLen( $_ipt ); //(1汉字=2字符)。返回长度。不判断违规字符 var txt = verifyReg.substringLen( $_ipt.val(),2000 ); //(1汉字=2字符)。限定2000字符(多出来的被截掉) if( verifyReg.verifyName( $_ipt ) === false ){alert("请检查。只允许字符:(汉字 a-z A-Z 0-9)");} */
verifyReg
/js/log.js 代码