1、js验证身份证信息是否合法
//js验证身份证信息是否合法 function validateCard(pId, field){ var arrVerifyCode = [1,0,"x",9,8,7,6,5,4,3,2]; var Wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]; var Checker = [1,9,8,7,6,5,4,3,2,1,1]; if(pId.length != 15 && pId.length != 18){ return false; } var Ai=pId.length==18 ? pId.substring(0,17) : pId.slice(0,6)+"19"+pId.slice(6,16); if (!/^\d+$/.test(Ai)){ return false; } var yyyy=Ai.slice(6,10) , mm=Ai.slice(10,12)-1 , dd=Ai.slice(12,14); var d=new Date(yyyy,mm,dd) , now=new Date(); var year=d.getFullYear() , mon=d.getMonth() , day=d.getDate(); if (year!=yyyy || mon!=mm || day!=dd || d>now || year<1900){ return false; } for(var i=0,ret=0;i<17;i++) ret+=Ai.charAt(i)*Wi[i]; Ai+=arrVerifyCode[ret %=11]; return pId.length ==18 && pId != Ai?false:true;
2、判断浏览器类型及版本
//判断浏览器类型及版本 function CheckBrowser() { ua = navigator.userAgent; ua = ua.toLocaleLowerCase(); var browserVersion; if (ua.match(/msie/) != null || ua.match(/trident/) != null) { browserType = "IE"; //哈哈,现在可以检测ie11.0了! browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1]; } else if (ua.match(/firefox/) != null) { browserType = "火狐"; } else if (ua.match(/opera/) != null) { browserType = "欧朋"; } else if (ua.match(/chrome/) != null) { browserType = "谷歌"; } else if (ua.match(/safari/) != null) { browserType = "Safari"; } var arr = new Array(browserType, browserVersion); return arr; }
3、数定转化为大写形式
//转换为大写 function numberToCN(numberValue){ var numberValue=new String(Math.round(numberValue*100)); // 数字金额 var chineseValue=""; // 转换后的汉字金额 var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字 var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位 var len=numberValue.length; // numberValue 的字符串长度 var Ch1; // 数字的汉语读法 var Ch2; // 数字位的汉字读法 var nZero=0; // 用来计算连续的零值的个数 var String3; // 指定位置的数值 if(len>15){ return "超出计算范围"; } if (numberValue==0){ chineseValue = "零元整"; return chineseValue; } String2 = String2.substr(String2.length-len, len); // 取出对应位数的STRING2的值 for(var i=0; i<len; i++){ String3 = parseInt(numberValue.substr(i, 1),10); // 取出需转换的某一位的值 if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) ){ if ( String3 == 0 ){ Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else if ( String3 != 0 && nZero != 0 ){ Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else{ Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } } else{ // 该位是万亿,亿,万,元位等关键位 if( String3 != 0 && nZero != 0 ){ Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if ( String3 != 0 && nZero == 0 ){ Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if( String3 == 0 && nZero >= 3 ){ Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else{ Ch1 = ""; Ch2 = String2.substr(i, 1); nZero = nZero + 1; } if( i == (len - 11) || i == (len - 3)){ // 如果该位是亿位或元位,则必须写上 Ch2 = String2.substr(i, 1); } } chineseValue = chineseValue + Ch1 + Ch2; } if ( String3 == 0 ){ // 最后一位(分)为0时,加上“整” chineseValue = chineseValue + "整"; } return chineseValue; }
3、实现千元分隔符
function formatNumber(num){ if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){ return num; } var a = RegExp.$1,b = RegExp.$2,c = RegExp.$3; var re = new RegExp().compile("(\\d)(\\d{3})(,|$)"); while(re.test(b)){ b = b.replace(re,"$1,$2$3"); } return a +""+ b +""+ c; }
4、时间日期格式转换
Date.prototype.Format = function(formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', '四', '五', '六']; str = str.replace(/yyyy|YYYY/, this.getFullYear()); str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)); str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1)); str = str.replace(/M/g, (this.getMonth() + 1)); str = str.replace(/w|W/g, Week[this.getDay()]); str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate()); str = str.replace(/d|D/g, this.getDate()); str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours()); str = str.replace(/h|H/g, this.getHours()); str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes()); str = str.replace(/m/g, this.getMinutes()); str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds()); str = str.replace(/s|S/g, this.getSeconds()); return str }
5、判断是否为数字类型
function isDigit(value) { var patrn = /^[0-9]*$/; if (patrn.exec(value) == null || value == "") { return false } else { return true } }
6、设置cookie值
function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; var utc = d.getTime() + (d.getTimezoneOffset() * 60000); var nd = utc + (3600000 * offset); var exp = new Date(nd); exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" }
7、获取cookie值
function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null }
8、加入收藏夹
function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sTitle) } catch(e) { try { window.sidebar.addPanel(sTitle, sURL, "") } catch(e) { alert("加入收藏失败,请使用Ctrl+D进行添加") } } }
9、设为首页
function setHomepage() { if (document.all) { document.body.style.behavior = 'url(#default#homepage)'; document.body.setHomePage('http://w3cboy.com') } else if (window.sidebar) { if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect") } catch(e) { alert("该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true") } } var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); prefs.setCharPref('browser.startup.homepage', 'http://w3cboy.com') } }
10、日期格式化函数+调用方法
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond }; if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o){ if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length)); } return format; } alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
11、打开一个窗体通用方法
function openWindow(url,windowName,width,height){ var x = parseInt(screen.width / 2.0) - (width / 2.0); var y = parseInt(screen.height / 2.0) - (height / 2.0); var isMSIE= (navigator.appName == "Microsoft Internet Explorer"); if (isMSIE) { var p = "resizable=1,location=no,scrollbars=no,width="; p = p+width; p = p+",height="; p = p+height; p = p+",left="; p = p+x; p = p+",top="; p = p+y; retval = window.open(url, windowName, p); } else { var win = window.open(url, "ZyiisPopup", "top=" + y + ",left=" + x + ",scrollbars=" + scrollbars + ",dialog=yes,modal=yes,width=" + width + ",height=" + height + ",resizable=no" ); eval("try { win.resizeTo(width, height); } catch(e) { }"); win.focus(); } }