1 //去除字符串两边的空格
2 String.prototype.trim = function () {
3 return this.replace(/(^\s+)|(\s+$)/g, "");
4 }
5 //检测字符串是否为空
6 String.prototype.isEmpty = function () {
7 return !(/.?[^\s ]+/.test(this));
8 }
9 //检测值是否介于某两个指定的值之间
10 String.prototype.isBetween = function (val, min, max) {
11 return isNaN(val) == false && val >= min && val <= max;
12 }
13 //获取最大值或最小值
14 String.prototype.getBetweenVal = function (what) {
15 var val = this.split(',');
16 var min = val[0];
17 var max = val[1] == null ? val[0] : val[1];
18 if (parseInt(min) > parseInt(max)) {
19 min = max;
20 max = val[0];
21 }
22 return what == 'min' ? (isNaN(min) ? null : min) : (isNaN(max) ? null : max);
23 }
24 var validator = function (formObj) {
25 this.allTags = formObj.getElementsByTagName('*');
26 //字符串验证正则表达式
27 this.reg = new Object();
28 this.reg.english = /^[a-zA-Z0-9_\-]+$/;
29 this.reg.chinese = /^[\u0391-\uFFE5]+$/;
30 this.reg.number = /^[-\+]?\d+(\.\d+)?$/;
31 this.reg.integer = /^[-\+]?\d+$/;
32 this.reg.float = /^[-\+]?\d+(\.\d+)?$/;
33 this.reg.date = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
34 this.reg.email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
35 this.reg.url = /^(((ht|f)tp(s?))\:\/\/)[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
36 this.reg.phone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
37 this.reg.mobile = /^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(159\d{8}))$/;
38 this.reg.ip = /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/;
39 this.reg.zipcode = /^[1-9]\d{5}$/;
40 this.reg.qq = /^[1-9]\d{4,10}$/;
41 this.reg.msn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
42 this.reg.idcard = /(^\d{15}$)|(^\d{17}[0-9Xx]$)/;
43 //错误输出信息
44 this.tip = new Object();
45 this.tip.unknow = '未找到的验证类型,无法执行验证。';
46 this.tip.paramError = '参数设置错误,无法执行验证。';
47 this.tip.required = '不允许为空。';
48 this.tip.english = '仅允许英文字符及下划线 (a-zA-Z0-9_)。';
49 this.tip.chinese = '仅允许中文字符。';
50 this.tip.number = '不是一个有效的数字。';
51 this.tip.integer = '不是一个有效的整数。';
52 this.tip.float = '不是一个有效的浮点数。';
53 this.tip.date = '不是一个有效的日期格式。 (例如:2007-06-29)';
54 this.tip.email = '不是一个有效的电子邮件格式。';
55 this.tip.url = '不是一个有效的超链接格式。';
56 this.tip.phone = '不是一个有效的电话号码。';
57 this.tip.mobile = '不是一个有效的手机号码。';
58 this.tip.ip = '不是一个有效的IP地址。';
59 this.tip.zipcode = '不是一个有效的邮政编码。';
60 this.tip.qq = '不是一个有效的QQ号码。';
61 this.tip.msn = '不是一个有效的MSN帐户。';
62 this.tip.idcard = '不是一个有效的身份证号码。';
63 //获取控件名称
64 this.getControlName = function ()
65 {
66 return this.element.getAttribute('controlName') == null
67 ? '指定控件的值'
68 : this.element.getAttribute('controlName');
69 }
70 //设定焦点
71 this.setFocus = function (ele) {
72 try {
73 ele.focus();
74 } catch (e){}
75 }
76 //设置边框颜色
77 this.setBorderColor = function (ele) {
78 var borderColor = ele.currentStyle ?
79 ele.currentStyle.borderColor :
80 document.defaultView.getComputedStyle(ele, null)['borderColor'];
81 ele.style.borderColor = '#ff9900';
82 ele.onkeyup = function () {
83 this.style.borderColor = borderColor;
84 }
85 }
86 //输出错误反馈信息
87 this.feedback = function (type) {
88 try {
89 var msg = eval('this.tip.' + type) == undefined ?
90 type :
91 this.getControlName() + eval('this.tip.' + type);
92 } catch (e) {
93 msg = type;
94 }
95 this.setBorderColor(this.element);
96 alert(msg);
97 this.setFocus(this.element);
98 };
99 //执行字符串验证
100 this.validate = function () {
101 var v = this.element.value;
102 //验证是否允许非空
103 var required = this.element.getAttribute('required');
104 if (required != null && v.isEmpty()) {
105 this.feedback('required');
106 return false;
107 }
108 //验证是否合法格式
109 var dataType = this.element.getAttribute('dataType');
110 if (!v.isEmpty() && dataType != null && dataType.toLowerCase() != 'password') {
111 dataType = dataType.toLowerCase();
112 try {
113 if (!(eval('this.reg.' + dataType)).test(v)) {
114 this.feedback(dataType);
115 return false;
116 }
117 } catch(e) {
118 this.feedback('unknow');
119 return false;
120 }
121 }
122 //执行数据验证
123 var confirm = this.element.getAttribute('confirm');
124 if (confirm != null) {
125 try {
126 var data = eval('formObj.' + confirm + '.value');
127 if (v != data) {
128 alert('两次输入的内容不一致,请重新输入。');
129 this.setBorderColor(this.element);
130 this.setFocus(this.element);
131 return false;
132 }
133 } catch (e) {
134 this.feedback('paramError');
135 return false;
136 }
137 }
138 //验证数字大小
139 var dataBetween = this.element.getAttribute('dataBetween');
140 if (!v.isEmpty() && dataBetween != null) {
141 var min = dataBetween.getBetweenVal('min');
142 var max = dataBetween.getBetweenVal('max');
143 if (min == null || max == null) {
144 this.feedback('paramError');
145 return false;
146 }
147 if (!v.isBetween(v.trim(), min, max)) {
148 this.feedback(this.getControlName() + '必须是介于 ' + min + '-' + max + ' 之间的数字。');
149 return false;
150 }
151 }
152 //验证字符长度
153 var dataLength = this.element.getAttribute('dataLength');
154 if (!v.isEmpty() && dataLength != null) {
155 var min = dataLength.getBetweenVal('min');
156 var max = dataLength.getBetweenVal('max');
157 if (min == null || max == null) {
158 this.feedback('paramError');
159 return false;
160 }
161 if (!v.isBetween(v.trim().length, min, max)) {
162 this.feedback(this.getControlName() + '必须是 ' + min + '-' + max + ' 个字符。');
163 return false;
164 }
165 }
166 return true;
167 };
168 //执行初始化操作
169 this.init = function () {
170 for (var i=0; i<this.allTags.length; i++) {
171 if (this.allTags[i].tagName.toUpperCase() == 'INPUT' ||
172 this.allTags[i].tagName.toUpperCase() == 'SELECT' ||
173 this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
174 {
175 this.element = allTags[i];
176 if (!this.validate())
177 return false;
178 }
179 }
180 };
181 return this.init();
182 }
2 String.prototype.trim = function () {
3 return this.replace(/(^\s+)|(\s+$)/g, "");
4 }
5 //检测字符串是否为空
6 String.prototype.isEmpty = function () {
7 return !(/.?[^\s ]+/.test(this));
8 }
9 //检测值是否介于某两个指定的值之间
10 String.prototype.isBetween = function (val, min, max) {
11 return isNaN(val) == false && val >= min && val <= max;
12 }
13 //获取最大值或最小值
14 String.prototype.getBetweenVal = function (what) {
15 var val = this.split(',');
16 var min = val[0];
17 var max = val[1] == null ? val[0] : val[1];
18 if (parseInt(min) > parseInt(max)) {
19 min = max;
20 max = val[0];
21 }
22 return what == 'min' ? (isNaN(min) ? null : min) : (isNaN(max) ? null : max);
23 }
24 var validator = function (formObj) {
25 this.allTags = formObj.getElementsByTagName('*');
26 //字符串验证正则表达式
27 this.reg = new Object();
28 this.reg.english = /^[a-zA-Z0-9_\-]+$/;
29 this.reg.chinese = /^[\u0391-\uFFE5]+$/;
30 this.reg.number = /^[-\+]?\d+(\.\d+)?$/;
31 this.reg.integer = /^[-\+]?\d+$/;
32 this.reg.float = /^[-\+]?\d+(\.\d+)?$/;
33 this.reg.date = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
34 this.reg.email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
35 this.reg.url = /^(((ht|f)tp(s?))\:\/\/)[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
36 this.reg.phone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
37 this.reg.mobile = /^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(159\d{8}))$/;
38 this.reg.ip = /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/;
39 this.reg.zipcode = /^[1-9]\d{5}$/;
40 this.reg.qq = /^[1-9]\d{4,10}$/;
41 this.reg.msn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
42 this.reg.idcard = /(^\d{15}$)|(^\d{17}[0-9Xx]$)/;
43 //错误输出信息
44 this.tip = new Object();
45 this.tip.unknow = '未找到的验证类型,无法执行验证。';
46 this.tip.paramError = '参数设置错误,无法执行验证。';
47 this.tip.required = '不允许为空。';
48 this.tip.english = '仅允许英文字符及下划线 (a-zA-Z0-9_)。';
49 this.tip.chinese = '仅允许中文字符。';
50 this.tip.number = '不是一个有效的数字。';
51 this.tip.integer = '不是一个有效的整数。';
52 this.tip.float = '不是一个有效的浮点数。';
53 this.tip.date = '不是一个有效的日期格式。 (例如:2007-06-29)';
54 this.tip.email = '不是一个有效的电子邮件格式。';
55 this.tip.url = '不是一个有效的超链接格式。';
56 this.tip.phone = '不是一个有效的电话号码。';
57 this.tip.mobile = '不是一个有效的手机号码。';
58 this.tip.ip = '不是一个有效的IP地址。';
59 this.tip.zipcode = '不是一个有效的邮政编码。';
60 this.tip.qq = '不是一个有效的QQ号码。';
61 this.tip.msn = '不是一个有效的MSN帐户。';
62 this.tip.idcard = '不是一个有效的身份证号码。';
63 //获取控件名称
64 this.getControlName = function ()
65 {
66 return this.element.getAttribute('controlName') == null
67 ? '指定控件的值'
68 : this.element.getAttribute('controlName');
69 }
70 //设定焦点
71 this.setFocus = function (ele) {
72 try {
73 ele.focus();
74 } catch (e){}
75 }
76 //设置边框颜色
77 this.setBorderColor = function (ele) {
78 var borderColor = ele.currentStyle ?
79 ele.currentStyle.borderColor :
80 document.defaultView.getComputedStyle(ele, null)['borderColor'];
81 ele.style.borderColor = '#ff9900';
82 ele.onkeyup = function () {
83 this.style.borderColor = borderColor;
84 }
85 }
86 //输出错误反馈信息
87 this.feedback = function (type) {
88 try {
89 var msg = eval('this.tip.' + type) == undefined ?
90 type :
91 this.getControlName() + eval('this.tip.' + type);
92 } catch (e) {
93 msg = type;
94 }
95 this.setBorderColor(this.element);
96 alert(msg);
97 this.setFocus(this.element);
98 };
99 //执行字符串验证
100 this.validate = function () {
101 var v = this.element.value;
102 //验证是否允许非空
103 var required = this.element.getAttribute('required');
104 if (required != null && v.isEmpty()) {
105 this.feedback('required');
106 return false;
107 }
108 //验证是否合法格式
109 var dataType = this.element.getAttribute('dataType');
110 if (!v.isEmpty() && dataType != null && dataType.toLowerCase() != 'password') {
111 dataType = dataType.toLowerCase();
112 try {
113 if (!(eval('this.reg.' + dataType)).test(v)) {
114 this.feedback(dataType);
115 return false;
116 }
117 } catch(e) {
118 this.feedback('unknow');
119 return false;
120 }
121 }
122 //执行数据验证
123 var confirm = this.element.getAttribute('confirm');
124 if (confirm != null) {
125 try {
126 var data = eval('formObj.' + confirm + '.value');
127 if (v != data) {
128 alert('两次输入的内容不一致,请重新输入。');
129 this.setBorderColor(this.element);
130 this.setFocus(this.element);
131 return false;
132 }
133 } catch (e) {
134 this.feedback('paramError');
135 return false;
136 }
137 }
138 //验证数字大小
139 var dataBetween = this.element.getAttribute('dataBetween');
140 if (!v.isEmpty() && dataBetween != null) {
141 var min = dataBetween.getBetweenVal('min');
142 var max = dataBetween.getBetweenVal('max');
143 if (min == null || max == null) {
144 this.feedback('paramError');
145 return false;
146 }
147 if (!v.isBetween(v.trim(), min, max)) {
148 this.feedback(this.getControlName() + '必须是介于 ' + min + '-' + max + ' 之间的数字。');
149 return false;
150 }
151 }
152 //验证字符长度
153 var dataLength = this.element.getAttribute('dataLength');
154 if (!v.isEmpty() && dataLength != null) {
155 var min = dataLength.getBetweenVal('min');
156 var max = dataLength.getBetweenVal('max');
157 if (min == null || max == null) {
158 this.feedback('paramError');
159 return false;
160 }
161 if (!v.isBetween(v.trim().length, min, max)) {
162 this.feedback(this.getControlName() + '必须是 ' + min + '-' + max + ' 个字符。');
163 return false;
164 }
165 }
166 return true;
167 };
168 //执行初始化操作
169 this.init = function () {
170 for (var i=0; i<this.allTags.length; i++) {
171 if (this.allTags[i].tagName.toUpperCase() == 'INPUT' ||
172 this.allTags[i].tagName.toUpperCase() == 'SELECT' ||
173 this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
174 {
175 this.element = allTags[i];
176 if (!this.validate())
177 return false;
178 }
179 }
180 };
181 return this.init();
182 }