常用JS工具包
1 /*********** 2 *时间辅助类 3 ***********/ 4 var DateHelper = { 5 //得到两个时间的差值(天数) 6 DateDiff: function (startDate, endDate) { 7 var strSeparator = "-"; //日期分隔符 8 var oDate1; 9 var oDate2; 10 var iDays; 11 oDate1 = startDate.split(strSeparator); 12 oDate2 = endDate.split(strSeparator); 13 var strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2]); 14 var strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2]); 15 iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)//把相差的毫秒数转换为天数 16 return iDays; 17 }, 18 //比较时间大小 19 DateCompare: function (startDate, endDate) { 20 var result = false; 21 if (new Date(endDate) > new Date(startDate)) { 22 result = true; 23 } 24 return result; 25 } 26 }; 27 /*********** 28 *常用验证类 29 ***********/ 30 var Validator = { 31 // 中文 32 isChinese: function (s) { 33 var p = /^[\u4e00-\u9fa5]+$/; 34 return this.test(s, p); 35 }, 36 // 英文 37 isEnglish: function (s) { 38 var p = /^[A-Za-z]+$/; 39 return this.test(s, p); 40 }, 41 // 邮箱 42 isEmail: function (s) { 43 var p = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 44 return this.test(s, p); 45 }, 46 47 48 // 手机号码 49 isMobile: function (s) { 50 return this.test(s, /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/); 51 }, 52 // 电话号码 53 isPhone: function (s) { 54 return this.test(s, /^[0-9]{3,4}\-[0-9]{7,8}$/); 55 }, 56 // 邮编 57 isPostCode: function (s) { 58 return this.test(s, /^[1-9][0-9]{5}$/); 59 }, 60 // 数字 61 isNumber: function (s, d) { 62 return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$')); 63 }, 64 // 判断是否为空 65 isEmpty: function (s) { 66 return jQuery.isEmptyObject(s) || (s == "" ? true : false); 67 }, 68 //判断是否是图片 69 isImage: function (s) { 70 return this.test(s, /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/); 71 }, 72 // 正则匹配 73 test: function (s, p) { 74 s = s.nodeType == 1 ? s.value : s; 75 return new RegExp(p).test(s); 76 } 77 }; 78 79 //去除字符串头尾空格或指定字符 80 String.prototype.Trim = function (c) { 81 if (c == null || c == "") { 82 var str = this.replace(/^s*/, ''); 83 var rg = /s/; 84 var i = str.length; 85 while (rg.test(str.charAt(--i))); 86 return str.slice(0, i + 1); 87 } 88 else { 89 var rg = new RegExp("^" + c + "*"); 90 var str = this.replace(rg, ''); 91 rg = new RegExp(c); 92 var i = str.length; 93 while (rg.test(str.charAt(--i))); 94 return str.slice(0, i + 1); 95 } 96 } 97 98 //去除字符串头部空格或指定字符 99 String.prototype.TrimStart = function (c) { 100 if (c == null || c == "") { 101 var str = this.replace(/^s*/, ''); 102 return str; 103 } 104 else { 105 var rg = new RegExp("^" + c + "*"); 106 var str = this.replace(rg, ''); 107 return str; 108 } 109 } 110 111 //去除字符串尾部空格或指定字符 112 String.prototype.trimEnd = function (c) { 113 if (c == null || c == "") { 114 var str = this; 115 var rg = /s/; 116 var i = str.length; 117 while (rg.test(str.charAt(--i))); 118 return str.slice(0, i + 1); 119 } 120 else { 121 var str = this; 122 var rg = new RegExp(c); 123 var i = str.length; 124 while (rg.test(str.charAt(--i))); 125 return str.slice(0, i + 1); 126 } 127 }
//修改字符长度,超过者以...代替 String.prototype.FixedLenth = function (lenth, ext) { var str = this; if (length == null || length == "") { length = 10; } if (ext == null || ext == "") { ext = ".."; } if (str.length > length) { str = str.substring(0, 10) + ext; } return str; }
平时常用的工具类,会持续维护此工具类