1:博客说明: 此文章 主要结合上一篇文章消息统一管理功能 写一个公共的js验证方法,从而对 用户提交的表单数据进行验证
验证包括 :字符串长度 电话号码 传真 邮箱 是否为数字等
1 /** 2 * ①IsEmpty 判断字符串是否为空 IsEmpty("chengminglian"); 3 * ②IsEmail 判断字符串是否为合法邮件地址 IsEmail("yuansiyong223@126.com",false); 4 * ③IsUrl 判断字符串是否为合法URL地址 IsUrl("yuansiyong223@126.com",false); 5 * ④IsPhone 正则表达式电话的验证 IsPhone("02925366958",false); 6 * ⑤IsAllowLength 判断字符串长度是否在给定长度范围内 IsAllowLength("testlength",20); 7 * ⑥IsPostNo 判断字符串是否为邮政编码 IsPostNo("131121",true); 8 * ⑦IsFaxNo 判断字符串是否为传真 IsFaxNo("710075",true); 9 * ⑧IsNumber 判断字符串是否为数字 IsNumber("123",false); 10 * ⑨IsEqual 判断两个字符串是否相等 IsEqual("123","456"); 11 * ValidateCheck 统一表单验证方法 ValidateCheck("teststr",false,20,allowType.Default,replaceChar); 12 */ 13 var CommonValidator ={ 14 allowType: { 15 Default: "", 16 Email: "Email", 17 Url: "Url", 18 Phone: "Phone", 19 PostNo: "PostNo", 20 FaxNo: "FaxNo ", 21 Number: 'Number', 22 IsSignNumber: 'SignNumber' 23 }, 24 /** 25 * 判断字符串是否为空 26 * @参数: {str} 验证对象 27 * @return:true:验证成功 true:验证失败 28 */ 29 IsEmpty: function (str) { 30 if (str.length == 0) { 31 return false; 32 } 33 return true; 34 }, 35 /** 36 * 判断字符串是否为合法邮件地址 37 * @参数: {str} 验证对象 38 * @参数: {allowNull} 是否允许为空 39 * @return:消息 40 */ 41 IsEmail: function (str, allowNull) { 42 if (str.length == 0) return allowNull; 43 var myReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; 44 if (myReg.test(str) == false) { 45 return false; 46 } 47 return true; 48 }, 49 /** 50 * 判断url是否为正确的URl地址 51 * @参数: {str} 验证对象 52 * @参数: {allowNull} 是否允许为空 53 * @return:消息 54 */ 55 IsUrl: function (str, allowNull) { 56 if (str.length == 0) return allowNull; 57 var myReg = "^((https|http|ftp|rtsp|mms)://)?[a-z0-9A-Z]{3}\.[a-z0-9A-Z][a-z0-9A-Z]{0,61}?[a-z0-9A-Z]\.com|net|cn|cc (:s[0-9]{1-4})?/$"; 58 var tempReg = new RegExp(myReg); 59 if (tempReg.test(str) == false) { 60 return false; 61 } else { 62 return true; 63 } 64 }, 65 /** 66 * 正则表达式电话的验证 67 * @参数: {str} 验证对象 68 * @参数: {allowNull} 是否允许为空 69 * @return:消息 70 */ 71 IsPhone: function (str, allowNull) { 72 if (str.length == 0) return allowNull; 73 var myReg = /(^[0-9]{3,4}\-[0-9]{5,8}$)|(^[0-9]{5,12}$)|(^\([0-9]{3,4}\)[0-9]{5,8}$)|(^0{0,1}13[0-9]{9}$)/; 74 if (myReg.test(str) == false) { 75 return false; 76 } 77 return true; 78 }, 79 /** 80 * 验证字符串长度是否在给定长度范围内 81 * @参数: {str} 验证对象 82 * @参数: {allowLength} 给定长度 83 * @return:消息 84 */ 85 IsAllowLength: function (str, allowLength) { 86 if (allowLength == 0) 87 return true; 88 var tempLength = 0; 89 var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; 90 if (regEx.test(str)) { 91 tempLength = str.length * 2; 92 } else { 93 var oMatches = str.match(/[\x00-\xff]/g); 94 var oLength = str.length; 95 if (oMatches != null || oMatches != undefined) { 96 oLength = oLength * 2 - oMatches.length; 97 } 98 tempLength = oLength; 99 } 100 if (tempLength > allowLength) { 101 return false; 102 } 103 return true; 104 }, 105 /** 106 * 判断字符串是否为合法邮政编码 107 * @参数: {str} 验证对象 108 * @参数: {allowNull} 是否允许为空 109 * @return:消息 110 */ 111 IsPostNo: function (str, allowNull) { 112 if (str.length == 0) return allowNull; 113 var myReg = /^[0-9]{6}$/; 114 if (myReg.test(str) == false) { 115 return false; 116 } 117 return true; 118 }, 119 /** 120 * 判断字符串是否为合法传真 121 * @参数: {str} 验证对象 122 * @参数: {allowNull} 是否允许为空 123 * @return:消息 124 */ 125 IsFaxNo: function (str, allowNull) { 126 if (str.length == 0) return allowNull; 127 var myReg = /^(\d{3,4}-)?\d{7,8}$/; 128 if (myReg.test(str) == false) { 129 return false; 130 } 131 return true; 132 }, 133 /** 134 * 判断字符串是否为数字 135 * @参数: {str} 验证对象 136 * @参数: {allowNull} 是否允许为空 137 * @return:消息 138 */ 139 IsNumber: function (str, allowNull) { 140 if (str.length == 0) return allowNull; 141 var myReg = /^[0-9]*$/; 142 if (myReg.test(str) == false) { 143 return false; 144 } 145 return true; 146 }, 147 /** 148 * 判断字符串是否为负整数 149 * @参数: {str} 验证对象 150 * @参数: {allowNull} 是否允许为空 151 * @return:消息 152 */ 153 IsSignNumber: function (str, allowNull) { 154 if (str.length == 0) return allowNull; 155 var myReg = /^\-?[0-9]+(.[5]{1})?$/; 156 if (myReg.test(str) == false) { 157 return false; 158 } 159 return true; 160 }, 161 /** 162 * 判断两个字符串是否一样 163 * @参数: {str1} 验证对象1 164 * @参数: {str2} 验证对象2 165 * @参数: {replaceChar} 验证失败时弹出消息 166 * @return:true:验证成功 true:验证失败 167 */ 168 IsEqual: function (str1, str2, replaceChar) { 169 if (str1 == str2) { 170 return true; 171 } 172 else { 173 MessageOperationApp.ShowMessage("W0007", replaceChar); 174 return false; 175 } 176 }, 177 /** 178 * 判断字符串是否为合法传真 179 * @参数: {str} 验证对象 180 * @参数: {allowNull} 是否允许为空 181 * @参数: {allowLength} 允许长度 182 * @参数: {allowType} 验证类型 183 * @参数: {replaceChar} 替换字符串 184 * @return:消息 185 */ 186 ValidateCheck: function (str, allowNull, allowLength, allowType, replaceChar) { 187 //为空验证 188 if (str.length == 0 && !allowNull) { 189 MessageOperationApp.ShowMessage("W0001", replaceChar); 190 return false; 191 } 192 //类型验证 193 //***********EMail邮件地址验证***************/ 194 if (allowType == this.allowType.Email) { 195 if (!this.IsEmail(str, allowNull)) { 196 MessageOperationApp.ShowMessage("W0003", replaceChar); 197 return false; 198 } 199 } 200 //***********Url地址验证*********************/ 201 if (allowType == this.allowType.Url) { 202 if (!this.IsUrl(str, allowNull)) { 203 MessageOperationApp.ShowMessage("W0003", replaceChar); 204 return false; 205 } 206 } 207 //***********Phone验证***********************/ 208 if (allowType == this.allowType.Phone) { 209 if (!this.IsPhone(str, allowNull)) { 210 MessageOperationApp.ShowMessage("W0003", replaceChar); 211 return false; 212 } 213 } 214 //***********PostNo邮政编码验证**************/ 215 if (allowType == this.allowType.PostNo) { 216 if (!this.IsPostNo(str, allowNull)) { 217 MessageOperationApp.ShowMessage("W0003", replaceChar); 218 return false; 219 } 220 } 221 //***********FaxNo传真验证*******************/ 222 if (allowType == this.allowType.FaxNo) { 223 if (!this.IsFaxNo(str, allowNull)) { 224 MessageOperationApp.ShowMessage("W0003", replaceChar); 225 return false; 226 } 227 } 228 //***********Number数字验证*******************/ 229 if (allowType == this.allowType.Number) { 230 if (!this.IsNumber(str, allowNull)) { 231 MessageOperationApp.ShowMessage("W0003", replaceChar); 232 return false; 233 } 234 } 235 //***********SignNumber数字验证*******************/ 236 if (allowType == this.allowType.IsSignNumber) { 237 if (!this.IsSignNumber(str, allowNull)) { 238 MessageOperationApp.ShowMessage("W0003", replaceChar); 239 return false; 240 } 241 } 242 //长度验证 243 if (!this.IsAllowLength(str, allowLength)) { 244 MessageOperationApp.ShowMessage("W0002", replaceChar); 245 return false; 246 } 247 return true; 248 } 249 }