日期:2014-05-16  浏览次数:20471 次

JS公共函数.

// JScript 文件 /* =========== JS 公共函数 080827 ====== */ /*

?========= 功能: =========== */

function IfShow(ShowMsg) { if (ShowMsg == "" ) { return false; } else { return true; } }

/* ========== 功能:替换空格(将多个连续空格替换为一个空格)使用:ReplaceSpace(obj) 返回:string = */

?

function ReplaceSpace(str) { while ( str.indexOf(" ") >= 0 ) { str = str.replace(" " ," "); } return str; }

/* ====== 功能:非空检查,不忽略空格提示信息:输入框为空,请输入!使用:isNull(obj,string) 返回:bool */

?function isNull(obj,ShowMsg) { var show = IfShow(ShowMsg) ; //非空检查 if(obj.value == "") { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } else { return true; } } /* ================================================================== 功能:邮箱地址检查提示信息:未输入邮件地址或邮件地址无效!使用:MailCheck(obj,string) 返回:bool ================================================================== */

function MailCheck(obj,ShowMsg) { var show = IfShow(ShowMsg) ; if(obj.value!= "") { var ok1=obj.value.indexOf("@"); var ok2=obj.value.indexOf("."); if(!((ok1!=-1)&&(ok2!=-1))) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } var allowstrlist = "&#%<>"; var endvalue = true; for (i=0;i 0) ) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } if (idCardLength==18) { sBirthday=obj.value.substr(6,4)+"-"+Number(obj.value.substr(10,2))+"-"+Number(obj.value.subs tr(12,2)); var d = new Date(sBirthday.replace(/-/g,"/")) if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(obj.value.charAt(17 - i),11); if(iSum%11!=1) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } } else if (idCardLength==15) { sBirthday = "19" + obj.value.substr(6,2) + "-" + Number(obj.value.substr(8,2)) + "-" + Number(obj.value.substr(10,2)); var d = new Date(sBirthday.replace(/-/g,"/")) var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate(); if(sBirthday != dd) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } } return true; }

/* ================================================================== 功能:验证电话号码格式是否正确提示信息:未输入或输入电话号码格式不正确!使用:isPhoneNo(obj,string) 返回:bool ================================================================== */

function isPhoneNo(obj,ShowMsg) { var show = IfShow(ShowMsg); var phoneNo = obj.value; var Endvalue = true; var allowstrlist = "1234567890()-"; if(phoneNo!="") { for (i=0;i obj.focus(); obj.select(); return false; break; } var r=objStr.match(reg); if(r==null) { if (show) alert(ShowMsg); obj.focus(); obj.select(); return false; } else { return true; } } /* ================================================================== 功能:字符串操作,去除字符串两边的空格使用:LTrim(string) 返回:string ================================================================== */ function LTrim(str) { var whitespace = new String(" \t\n\r"); var s = new String(str); if (whitespace.indexOf(s.charAt(0)) != -1) { var j=0, i = s.length; while (j < i && whitespace.indexOf(s.charAt(j)) != -1) { j++; } s = s.substring(j, i); } return s; } /* ================================================================== 功能:去除右边的空格 使用:RTrim(string) 返回:string ================================================================== */ function RTrim(str) { var whitespace = new String(" \t\n\r"); var s = new String(str); if (whitespace.indexOf(s.charAt(s.length-1)) != -1) { var i = s.length - 1; while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) { i--; } s = s.substring(0, i+1); } return s; } //去除前后空格 function Trim(str) { return RTrim(LTrim(str)); } /* ================================================================== 功能:无效字符的检测(不允许输入特殊字符)提示信息:未输入或输入包含非法字符使用:CheckChar(obj,Lchar,string) Lchar:要检查的特殊字符返回:bool ================================================================== */ function CheckChar(obj,Lchar,Sh