var fmtDate = /^(\d{1,4})-(\d{1,2})-(\d{1,2})\b/; // 日期 //var fmtEmail = /^[\w-\.]+@[\w-]+\.[\w-]+$/; // Email var fmtEmail = /^[\w-\.]+@[\w-]+(\.[\w-]+)+$/; // Email var fmtMobilePhone = /^13\d{9}$/; // 移動電話 var fmtTelephone = /^(0\d{2,3}-?)?\d{7,8}$/; // 國內固定電話 var fmtZip = /^[1-9]\d{5}$/; // 郵政編碼 var fmtTrim = /^[\s ]*([\S]*)[\s ]*$/; // trim var fmtTime = /^(\d{1,2})\:(\d{1,2})$/; var fmtQuote = /\‘/g;
/** 僅能判斷字符串去除空格后是否為空,因為字符串中間有空格或者其他字符時返回整個字符串 */ String.prototype.trim = function (){ return this.replace(fmtTrim, ‘$1‘); }
/** 將 ‘ 替換成 ‘‘ ,SQL 語句用 --還沒有用上 */ String.prototype.quote = function (){ return this.replace(fmtQuote, ‘\‘\‘‘); }
/** 字節(jié)長度 */ String.prototype.byteLen = function (){ var count = 0, idx = 0; while (idx < this.length) count += this.charCodeAt(idx++) > 0xFF ? 2 : 1; return count; }
/** 返回日期的毫秒數(shù)(符合fmtDate描述的日期) 日期格式y(tǒng)yyy-mm-dd */ function parseDate(date){ return Date.parse(date.replace(fmtDate, ‘$2/$3/$1‘)); }
/** 比較日期 */ function compareDate(date1, date2){ return parseDate(date1) - parseDate(date2); }
/** 是否日期(嚴格判斷用isValidDate()) */ function isDate(date){ return fmtDate.test(date); }
/** 是否時間 hh:mm */ function isTime(strTime){ return fmtTime.test(strTime) && RegExp.$1 < 60 && RegExp.$2 < 60; }
/** 是否Email */ function isEmail(email){ return fmtEmail.test(email); }
/** 是否手機號碼(13xxxxxxxxx) */ function isMobilePhone(mobilePhone){ return fmtMobilePhone.test(mobilePhone); }
/** 是否固定電話 */ function isTelephone(telephone){ return fmtTelephone.test(telephone); }
/** 固定電話或者移動電話 */ function isTel(tel){ return isMobilePhone(tel) || isTelephone(tel); }
/** 郵政編碼 */ function isZip(zip){ return fmtZip.test(zip); }
/** 數(shù)字域,但允許為空 */ function numField(field){ with (field){ if (value.trim() != ‘‘ && isNaN(value)){ alert(‘請輸入數(shù)字!‘); focus(); select(); return false; } else return true; } }
/** 電話域 */ function telField(field){ with (field){ if (value.trim() != ‘‘ && !isTel(value)){ alert(‘請輸入有效電話號碼!‘); focus(); select(); return false; } else return true; } }
/** 郵政編碼域 */ function zipField(field){ with (field){ if (value.trim() != ‘‘ && !isZip(value)){ alert(‘請輸入有效郵政編碼!‘); focus(); select(); return false; } else return true; } }
/** Email 域 */ function emailField(field){ with (field){ if (value.trim() != ‘‘ && !isEmail(value)){ alert(‘請輸入有效Email!‘); focus(); select(); return false; &;#160; } else return true; } }
/** Date 域 */ function dateField(field){ with (field){ if (value.trim() != ‘‘ && !isValidDate(value)){ alert(‘請輸入有效日期!‘); focus(); select(); return false; } else return true; } }
/** Time 域 (hh:mm) */ function timeField(field){ with (field){ if (value.trim() != ‘‘ && !isTime(value)){ alert(‘請輸入有效時間(hh:mm)!‘); focus(); select(); return false; } else return true; } }
/** 用于去除月份、日前導的0,否則 parseInt() 認為是8進數(shù)值,出錯 */ function trimZero(bin){ var idx = 0; while (bin.charCodeAt(idx) == 48) idx++; //48 == ‘0‘.charCode();
return bin.substring(idx); }
/** 是否潤年 */ function isLeapYear(iYear){ if (iYear % 4 == 0){ if (iYear % 100 == 0 && iYear % 400 != 0) return false; else return true; } else return false; }
/** 是否合法日期 */ function isValidDate2(iYear, iMonth, iDay){
switch (iMonth){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (iDay < 32) return true; case 4: case 6: case 9: case 11: if (iDay < 31) return true; case 2: if (isLeapYear(iYear) && iDay < 30) return true; else if (iDay < 29) return true; default: return false; } }
/** 是否合法日期 */ function isValidDate(sDate){ if (fmtDate.exec(sDate) == null) return false;
with (RegExp){ return isValidDate2(parseInt(trimZero($1)), parseInt(trimZero($2)), parseInt(trimZero($3))); } }
/** 當前月 */ function isCurrentMonth(sDate){ var date = new Date(); var aMonth = date.getMonth() + 1; var aYear = date.getFullYear();
date.setTime(parseDate(sDate)); var bMonth = date.getMonth() + 1; var bYear = date.getYear();
return aMonth == bMonth && aYear == bYear; } |