常用的代码
//获取距离今天的某一天日期 getDateStr: function (AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = this.zero(dd.getMonth() + 1);//获取当前月份的日期 var d = this.zero(dd.getDate()); return y + "-" + m + "-" + d; }, //判断字符串是否为空 isNull: function (str) { if ($.trim(str).length == 0)return true; else return false; }, //判断字符串是否为微信号 isWx: function (str) { var rex = /^[a-zA-Z\d_]{5,}$/; if (rex.test($.trim(str)))return true; return false; }, //判断是否全为数字 isNum: function (num) { var rex = /^[0-9]+$/; if (rex.test($.trim(num)))return true; return false; }, //判断是否全为热线电话 isHotline: function (num) { var rex = /^[0-9]([0-9]|-)*[0-9]$/; if (rex.test($.trim(num)))return true; return false; }, //判断是否全为单价 isPrice: function (num) { var rex = /^[0-9]+$/; //var num = n.trim(); var _this = this; var numArr = num.toString().split('.'); var len = numArr.length; var temp = 0; if (len > 2)return false; if (len == 2) { var l = numArr[1].length; var l1 = numArr[0].length; if (l > 2) { return false; } if (numArr[0].trim().length != l1)return false; } $.each(numArr, function (i) { if (numArr[i] < 0 || !_this.isNum(numArr[i])) { temp = 1 } }); if (temp)return false; return true; }, isMoney: function (n) { var rex = /^[0-9]+$/; var num = n.trim(); var _this = this; var numArr = num.toString().split('.'); var len = numArr.length; var temp = 0; if (len > 2)return false; if (len == 2) { var l = numArr[1].length; var l1 = numArr[0].length; if (l > 2) { return false; } if (numArr[0].trim().length != l1)return false; } $.each(numArr, function (i) { if (numArr[i] < 0 || !_this.isNum(numArr[i])) { temp = 1 } }); if (temp)return false; return true; }, //判断是否为url地址 isUrl: function (str) { var rex = /^(http|https){1}:\/\/[^\s]+$/; if (rex.test($.trim(str)))return true; else return false; }, //验证email格式 isEmail: function (str) { var reg = /^\w+((-w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; if (reg.test(str))return true; }, isPhone: function (str) { var a = /^1\d{10}$/, s = $.trim(str); if (a.test(s))return true; }, istel: function (str) { var rex = /^[0-9]{3,4}-[0-9]{7,8}$/; if (rex.test(str))return true; else return false; }, //验证银行卡号 //Description: 银行卡号Luhm校验 //Luhm校验规则:16位银行卡号(19位通用): //1.将未带校验位的 15(或18)位卡号从右依次编号 1 到 15(18),位于奇数位号上的数字乘以 2。 // 2.将奇位乘积的个十位全部相加,再加上所有偶数位上的数字。 // 3.将加法和加上校验位能被 10 整除。 //方法步骤很清晰,易理解,需要在页面引用Jquery.js //bankno为银行卡号 banknoInfo为显示提示信息的DIV或其他控件 isBankId: function (bankno) { var rex = /^[A-Za-z0-9]+$/; if (rex.test(bankno))return true; return false; var lastNum = bankno.substr(bankno.length - 1, 1);//取出最后一位(与luhm进行比较) var first15Num = bankno.substr(0, bankno.length - 1);//前15或18位 var newArr = []; for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组 newArr.push(first15Num.substr(i, 1)); } var arrJiShu = []; //奇数位*2的积 <9 var arrJiShu2 = []; //奇数位*2的积 >9 var arrOuShu = []; //偶数位数组 for (var j = 0; j < newArr.length; j++) { if ((j + 1) % 2 == 1) {//奇数位 if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2); else arrJiShu2.push(parseInt(newArr[j]) * 2); } else //偶数位 arrOuShu.push(newArr[j]); } var jishu_child1 = [];//奇数位*2 >9 的分割之后的数组个位数 var jishu_child2 = [];//奇数位*2 >9 的分割之后的数组十位数 for (var h = 0; h < arrJiShu2.length; h++) { jishu_child1.push(parseInt(arrJiShu2[h]) % 10); jishu_child2.push(parseInt(arrJiShu2[h]) / 10); } var sumJiShu = 0; //奇数位*2 < 9 的数组之和 var sumOuShu = 0; //偶数位数组之和 var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和 var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和 var sumTotal = 0; for (var m = 0; m < arrJiShu.length; m++) { sumJiShu = sumJiShu + parseInt(arrJiShu[m]); } for (var n = 0; n < arrOuShu.length; n++) { sumOuShu = sumOuShu + parseInt(arrOuShu[n]); } for (var p = 0; p < jishu_child1.length; p++) { sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]); sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]); } //计算总和 sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2); //计算Luhm值 var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10; var luhm = 10 - k; if (lastNum == luhm) { return true; } else { return false; } }, //验证是否中文 isCN: function (str) { var s = $.trim(str), reg = /^[\u4e00-\u9fa5]+$/; if (reg.test(s))return true; else return false; }, //验证密码格式 isPwd: function (str) { var reg = /^[A-Za-z0-9_-]+$/; if (reg.test(str))return true; }, isCard: function (str) { /* var reg = /^\d{17}[\d|x]|\d{15}$/; if(reg.test(str))return true; return false*/ //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X var reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/; if (reg.test(str.trim())) { return true; } return false; }, /** * @description 清除脚本内容 * @param s * @returns {String} */ stripscript: function (s) { return s.replace(/<script.*?>.*?<\/script>/ig, ''); }, //设置cookie setCookie: function (cname, cvalue, exdays) { var data = {path: '/'}; if (exdays) { data.expires = exdays; } $.cookie(cname, cvalue, data); }, //清除cookie clearCookie: function (name) { this.setCookie(name, null, -1); }, //获取cookie getCookie: function (cname) { return $.cookie(cname); },