-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbootstrap-validation.js
154 lines (137 loc) · 6.34 KB
/
bootstrap-validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* =========================================================
* bootstrap-validation.js
* Original Idea: http:/www.newkou.org (Copyright 2012 Stefan Petre)
* Updated by 不会飞的羊 (https://github.com/FateSheep/Validation-for-Bootstrap)
* =========================================================
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================= */
!function($) {
$.fn.validation = function(options) {
return this.each(function() {
globalOptions = $.extend({}, $.fn.validation.defaults, options);
validationForm(this)
});
};
$.fn.validation.defaults = {
validRules : [
{name: 'required', validate: function(value) {return ($.trim(value) == '');}, defaultMsg: '请输入内容。'},
{name: 'number', validate: function(value) {return (!/^[0-9]\d*$/.test(value));}, defaultMsg: '请输入数字。'},
{name: 'mail', validate: function(value) {return (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(value));}, defaultMsg: '请输入邮箱地址。'},
{name: 'char', validate: function(value) {return (!/^[a-z\_\-A-Z]*$/.test(value));}, defaultMsg: '请输入英文字符。'},
{name: 'chinese', validate: function(value) {return (!/^[\u4e00-\u9fff]$/.test(value));}, defaultMsg: '请输入汉字。'}
]
};
var formState = false, fieldState = false, wFocus = false, globalOptions = {};
var validateField = function(field, valid) { // 验证字段
var el = $(field), error = false, errorMsg = '';
for (i = 0; i < valid.length; i++) {
var x = true, flag = valid[i], msg = (el.attr(flag + '-message')==undefined)?null:el.attr(flag + '-message');;
if (flag.substr(0, 1) == '!') {
x = false;
flag = flag.substr(1, flag.length - 1);
}
var rules = globalOptions.validRules;
for (j = 0; j < rules.length; j++) {
var rule = rules[j];
if (flag == rule.name) {
if (rule.validate.call(field, el.val()) == x) {
error = true;
errorMsg = (msg == null)?rule.defaultMsg:msg;
break;
}
}
}
if (error) {break;}
}
var controls = el.parents('.controls'), controlGroup = el.parents('.control-group'), errorEl = controls.children('.help-block, .help-inline');
if (error) {
if (!controlGroup.hasClass('error')) {
if (errorEl.length > 0) {
var help = errorEl.text();
controls.data('help-message', help);
errorEl.text(errorMsg);
} else {
controls.append('<span class="help-inline">'+errorMsg+'</span>');
}
controlGroup.addClass('error');
}
} else {
if (fieldState) {
if (errorEl.length > 0) {
var help = controls.data('help-message');
if (help == undefined) {
errorEl.remove();
} else {
errorEl.text(help);
}
}
controlGroup.attr('class','control-group');
} else {
if (errorEl.length > 0) {
var help = errorEl.text();
controls.data('help-message', help);
}
}
}
return !error;
};
var validationForm = function(obj) { // 表单验证方法
$(obj).submit(function() { // 提交时验证
if (formState) { // 重复提交则返回
return false;
}
formState = true;
var validationError = false;
$('input, textarea', this).each(function () {
var el = $(this), valid = (el.attr('check-type')==undefined)?null:el.attr('check-type').split(' ');
if (valid != null && valid.length > 0) {
if (!validateField(this, valid)) {
if (wFocus == false) {
scrollTo(0, el[0].offsetTop - 50);
wFocus = true;
}
validationError = true;
}
}
});
wFocus = false;
fieldState = true;
if (validationError) {
formState = false;
$('input, textarea').each(function() {
var el = $(this), valid = (el.attr('check-type')==undefined)?null:el.attr('check-type').split(' ');
if (valid != null && valid.length > 0) {
el.focus(function() { // 获取焦点时
var controls = el.parents('.controls'), controlGroup = el.parents('.control-group'), errorEl = controls.children('.help-block, .help-inline');
if (errorEl.length > 0) {
var help = controls.data('help-message');
if (help == undefined) {
errorEl.remove();
} else {
errorEl.text(help);
}
}
controlGroup.attr('class','control-group');
});
el.blur(function() { // 失去焦点时
validateField(this, valid);
});
}
});
return false;
}
return true;
});
};
}(window.jQuery);