js数据验证集合、js email验证、js url验证、js长度验证、js数字验证等简单封装
/**
* 数据验证框架.增加了对id字段检查出错时,直接在对应后面添加一< span>元素来显示错误信息.
var checkData = new function() { var idExt="_wangzi6hao_Span";//生成span层的id后缀 }/**
this.length = function(str) { var p1 = new RegExp('%u..', 'g') var p2 = new RegExp('%.', 'g') return escape(str).replace(p1, '').replace(p2, '').length }
this.remove = function(id) { var idObject = document.getElementById(id); if (idObject != null) idObject.parentNode.removeChild(idObject); }
this.appendError = function(id, str) { this.remove(id + idExt);// 如果span元素存在,则先删除此元素 var spanNew = document.createElement("span");// 创建span spanNew.id = id + idExt;// 生成spanid spanNew.style.color = "red"; spanNew.appendChild(document.createTextNode(str));// 给span添加内容 var inputId = document.getElementById(id); inputId.parentNode.insertBefore(spanNew, inputId.nextSibling);// 给需要添加元素后面添加span }/**
this.trimSpace = function(str) { str += ""; while ((str.charAt(0) == ' ') || (str.charAt(0) == '???') || (escape(str.charAt(0)) == '%u3000')) str = str.substring(1, str.length); while ((str.charAt(str.length - 1) == ' ') || (str.charAt(str.length - 1) == '???') || (escape(str.charAt(str.length - 1)) == '%u3000')) str = str.substring(0, str.length - 1); return str; }/**
* * @param {Object} * inputString */ this.trim = function(inputString) { if (typeof inputString != "string") { return inputString; } var retValue = inputString; var ch = retValue.substring(0, 1); while (ch == " ") { // 检查字符串开始部分的空格 retValue = retValue.substring(1, retValue.length); ch = retValue.substring(0, 1); } ch = retValue.substring(retValue.length - 1, retValue.length); while (ch == " ") { // 检查字符串结束部分的空格 retValue = retValue.substring(0, retValue.length - 1); ch = retValue.substring(retValue.length - 1, retValue.length); } while (retValue.indexOf(" ") != -1) { // 将文字中间多个相连的空格变为一个空格 retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ") + 1, retValue.length); } return retValue; }/**
* * @param {Object} * str * @param {Object} * filterStr * * @return 包含过滤内容,返回True,否则返回false; */ this.filterStr = function(str, filterString) { filterString = filterString == "" ? "'~!@#$%^&*()-+."" : filterString var ch; var i; var temp; var error = false;// 当包含非法字符时,返回True for (i = 0; i <= (filterString.length - 1); i++) { ch = filterString.charAt(i); temp = str.indexOf(ch); if (temp != -1) { error = true; break; } } return error; } this.filterStrSpan = function(id, filterString) { filterString = filterString == "" ? "'~!@#$%^&*()-+."" : filterString var val = document.getElementById(id); if (this.filterStr(val.value, filterString)) { val.select(); var str = "不能包含非法字符" + filterString; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }
this.isURL = function(str_url) {// 验证url var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@ + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 + "|" // 允许IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 端口- :80 + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; var re = new RegExp(strRegex); return re.test(str_url); } this.isURLSpan = function(id) { var val = document.getElementById(id); if (!this.isURL(val.value)) { val.select(); var str = "链接不符合格式;"; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isEmail = function(str) { var re = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; return re.test(str); } this.isEmailSpan = function(id) { var val = document.getElementById(id); if (!this.isEmail(val.value)) { val.select(); var str = "邮件不符合格式;"; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isNum = function(str) { var re = /^[\d]+$/ return re.test(str); } this.isNumSpan = function(id) { var val = document.getElementById(id); if (!this.isNum(val.value)) { val.select(); var str = "必须是数字;"; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }
this.isRangeNum = function(str_num, small, big) { if (!this.isNum(str_num)) // 检查是否为数字 return false if (small == "" && big == "") throw str_num + "没有定义最大,最小值数字!"; if (small != "") { if (str_num < small) return false; } if (big != "") { if (str_num > big) return false; } return true; } this.isRangeNumSpan = function(id, small, big) { var val = document.getElementById(id); if (!this.isRangeNum(val.value, small, big)) { val.select(); var str = ""; if (small != "") { str = "应该大于或者等于 " + small; } if (big != "") { str += " 应该小于或者等于 " + big; } this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isLicit = function(str) { var re = /^[_0-9a-zA-Z]*$/ return re.test(str); } this.isLicitSpan = function(id) { var val = document.getElementById(id); if (!this.isLicit(val.value)) { val.select(); var str = "是由a-z0-9_组成的字符串(不区分大小写);"; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isEquals = function(str1, str2) { return str1 == str2; } this.isEqualsSpan = function(id, id1) { var val = document.getElementById(id); var val1 = document.getElementById(id1); if (!this.isEquals(val.value, val1.value)) { val.select(); var str = "二次输入内容必须一样;"; this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isRange = function(str, lessLen, moreLen) { var strLen = this.length(str); if (lessLen != "") { if (strLen < lessLen) return false; } if (moreLen != "") { if (strLen > moreLen) return false; } if (lessLen == "" && moreLen == "") throw "没有定义最大最小长度!"; return true; } this.isRangeSpan = function(id, lessLen, moreLen) { var val = document.getElementById(id); if (!this.isRange(val.value, lessLen, moreLen)) { var str = "长度"; if (lessLen != "") str += "大于或者等于 " + lessLen + ";"; if (moreLen != "") str += " 应该小于或者等于 " + moreLen; val.select(); this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isLess = function(str, lessLen) { return this.isRange(str, lessLen, ""); } this.isLessSpan = function(id, lessLen) { var val = document.getElementById(id); if (!this.isLess(val.value, lessLen)) { var str = "长度大于或者等于 " + lessLen; val.select(); this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isMore = function(str, moreLen) { return this.isRange(str, "", moreLen); } this.isMoreSpan = function(id, moreLen) { var val = document.getElementById(id); if (!this.isMore(val.value, moreLen)) { var str = "长度应该小于或者等于 " + moreLen; val.select(); this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } }/**
this.isEmpty = function(str) { return str == ""; } this.isEmptySpan = function(id) { var val = document.getElementById(id); if (this.isEmpty(val.value)) { var str = "不允许为空;"; val.select(); this.appendError(id, str); return false; } else { this.remove(id + idExt); return true; } } }