myjslib.js代码:
//去除字符串两边空格 String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); } //字符串去除左空格 String.prototype.ltrim = function(){ return this.replace(/(^\s*)/g,""); } //字符串去除右空格 String.prototype.rtrim = function(){ return this.replace(/(\s*$)/g, ""); } /** * 邮箱验证 * return true|false; */ String.prototype.ismail = function(){ var regex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; if(regex.test(this)){ return true; }else{ return false; } } /** * 身份证号验证 * return true(身份证号合法)|false(身份证号不合法) * */ String.prototype.checkidcardnum = function(){ var message = new Array("验证通过!","身份证号码位数不对!","身份证号码出生日期超出范围或含有非法字符!","身份证号码校验错误!","身份证地区非法!"); var area={ 11:"北京", 12:"天津", 13:"河北", 14:"山西", 15:"内蒙古", 21:"辽宁", 22:"吉林", 23:"黑龙江", 31:"上海", 32:"江苏", 33:"浙江", 34:"安徽", 35:"福建", 36:"江西", 37:"山东", 41:"河南", 42:"湖北", 43:"湖南", 44:"广东", 45:"广西", 46:"海南", 50:"重庆", 51:"四川", 52:"贵州", 53:"云南", 54:"西藏", 61:"陕西", 62:"甘肃", 63:"青海", 64:"宁夏", 65:"新疆", 71:"台湾", 81:"香港", 82:"澳门", 91:"国外" } var Y,JYM; var S,M; var idcard_array = new Array(); idcard_array = this.split(""); if(area[parseInt(this.substr(0,2))]==null){ //alert("错误信息"+message[4]); return false; } switch(this.length){ case 15: if ((parseInt(this.substr(6,2))+1900) % 4 == 0 || ((parseInt(this.substr(6,2))+1900) % 100 == 0 && (parseInt(this.substr(6,2))+1900) % 4 == 0 )){ ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性 }else{ ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性 } if(ereg.test(this)){ //alert("验证通过信息:"+message[0]); return true; }else{ //alert("错误信息:"+message[2]); return false; } break; case 18: if( parseInt(this.substr(6,4)) % 4 == 0 || ( parseInt(this.substr(6,4)) % 100 == 0 && parseInt(this.substr(6,4))%4 == 0 )){ ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式 }else{ ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式 } if(ereg.test(this)){ S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ; Y = S % 11; M = "F"; JYM = "10X98765432"; M = JYM.substr(Y,1); if(M == idcard_array[17]){ //alert("验证通过信息:"+message[0]); return true; }else{ //alert("错误信息:"+message[3]); return false; } }else{ //alert("错误信息:"+message[2]); return false; } break; default: //alert("错误信息:"+message[1]); return false; break; } } /** *解决下拉列表(宽度不够)内容太长,内容被遮挡问题 * -Start */ function FixWidth(selectObj) { if (navigator.userAgent.toLowerCase().indexOf("firefox") > 0) { return; } var newSelectObj = document.createElement("select"); newSelectObj = selectObj.cloneNode(true); newSelectObj.selectedIndex = selectObj.selectedIndex; newSelectObj.id = "newSelectObj"; var e = selectObj; var absTop = e.offsetTop; var absLeft = e.offsetLeft; while (e = e.offsetParent) { absTop += e.offsetTop; absLeft += e.offsetLeft; } with (newSelectObj.style) { position = "absolute"; top = absTop + "px"; left = absLeft + "px"; width = "auto"; } var rollback = function() { RollbackWidth(selectObj, newSelectObj); }; if (window.addEventListener) { newSelectObj.addEventListener("blur", rollback, false); newSelectObj.addEventListener("change", rollback, false); } else { newSelectObj.attachEvent("onblur", rollback); newSelectObj.attachEvent("onchange", rollback); } selectObj.style.visibility = "hidden"; document.body.appendChild(newSelectObj); var newDiv = document.createElement("div"); with (newDiv.style) { position = "absolute"; top = (absTop - 10) + "px"; left = (absLeft - 10) + "px"; width = newSelectObj.offsetWidth + 20; height = newSelectObj.offsetHeight + 20; ; background = "transparent"; } document.body.appendChild(newDiv); newSelectObj.focus(); var enterSel = "false"; var enter = function() { enterSel = enterSelect(); }; newSelectObj.onmouseover = enter; var leavDiv = "false"; var leave = function() { leavDiv = leaveNewDiv(selectObj, newSelectObj, newDiv, enterSel); }; newDiv.onmouseleave = leave; } function RollbackWidth(selectObj, newSelectObj) { selectObj.selectedIndex = newSelectObj.selectedIndex; selectObj.style.visibility = "visible"; if (document.getElementById("newSelectObj") != null) { document.body.removeChild(newSelectObj); } } function removeNewDiv(newDiv) { document.body.removeChild(newDiv); } function enterSelect() { return "true"; } function leaveNewDiv(selectObj, newSelectObj, newDiv, enterSel) { if (enterSel == "true") { RollbackWidth(selectObj, newSelectObj); removeNewDiv(newDiv); } } /** *解决下拉列表(宽度不够)内容太长,内容被遮挡问题 * -End */
HTML页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> JavaScript库个人积累总结 </title> <style type="text/css"> table tr td { border:1px solid #D8DEF0; cursor:default; } </style> <script type="text/javascript" language="javascript" src="js/myjslib.js"></script> <script type="text/javascript" language="javascript"> var $ = function(objectid){ return document.getElementById(objectid); } //标题颜色切换 function titColor(){ window.setInterval(function(){ if($("title").style.color=="yellow"){ $("title").style.color="red"; }else{ $("title").style.color="yellow"; } },500); } </script> </head> <body style="background:#E2F5FA;" onload="titColor();"> <center> <span id="title" style="color:yellow;font-size:16px;"><b>JavaScript方法积累</b></span> <table style="border:1px solid #D8DEF0;cellpadding:0px;cellspacing:0px;width:100%;"> <thead style="background:#F0EFE3;"> <th width="20%" align="center">功能</th> <th width="30%" align="center">示例</th> <th widht="50%" align="center">说明</th> </thead> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>1.字符串去左右空格</td> <td><input type="text" name="strtrim" id="strtrim" value=" hello "><input type="button" value="去两边空格" onclick="alert($('strtrim').value.trim());"></td> <td><pre><code>方法:String.trim();</code></pre></td> </tr> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>2.字符串去左空格</td> <td><input type="text" name="strtrim" id="strlefttrim" value=" hello "><input type="button" value="去左空格" onclick="alert($('strlefttrim').value.ltrim());"></td> <td>方法:String.ltrim();</td> </tr> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>3.字符串去右空格</td> <td><input type="text" name="strtrim" id="strrighttrim" value=" hello "><input type="button" value="去右空格" onclick="alert($('strrighttrim').value.rtrim());"></td> <td>方法:String.rtrim();</td> </tr> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>4.邮箱验证</td> <td><input type="text" name="mail" id="mail" value="[email protected]"><input type="button" value="验证" onclick="alert($('mail').value.ismail());"></td> <td>方法:String.ismail();</td> </tr> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>5.身份证号码验证</td> <td><input type="text" name="idcardnum" id="idcardnum" value="130124198808155008"><input type="button" value="验证" onclick="alert($('idcardnum').value.checkidcardnum());"></td> <td>方法:String.checkidcardnum();</td> </tr> <tr align="left" onmouseover="this.style.background='#FEFFE7';" onmouseout="this.style.background='#E2F5FA';"> <td>6.解决下拉列表内容长被遮挡问题</td> <td> <select name="Select1" style="width:80px;" onmouseover="FixWidth(this);"> <option id="A" title="this is A">AAAAAAAAAAAAAAA</option> <option id="B" title="this is B">BBBBBBBBBBBBBBB</option> <option id="C" title="this is C">CCCCCCCCCCCCCCC</option> </select> </td> <td>方法: onmouseover = FixWidth(this); </td> </tr> </table> </center> </body> </html>