-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
185 lines (131 loc) · 5.3 KB
/
validator.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(function($){
$.fn.extend({
//pass the options variable to the function
validator: function(options) {
//Set the default values, use comma to separate the settings, example:
var defaults = {
pattern : {
// tel : /^((\+\d{1,3}? ?\d{1,3}? ?[\d\- \(\)]{5,15})|([\-\ \(\)\d]{6,20}))$/,
tel : /^([ -\+\.\d\(\)ext]+){5,20}$/,
email : /^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/,
number : /^\d+$/,
url : /(https?:\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((\/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?/,
text : /./
},
classes : {
valid : 'valid',
error : 'error',
placeholder : 'placeholder'
},
errorMessageAttribute : 'error'
}
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
var v = {};
v.form = $(this);
v.submitBtn = $('[type=submit]', v.form);
v.clearBtn = $('[type=reset]', v.form);
v.requiredFields = $(':input[required=""],:input[required]', v.form);
v.placeholders = $(':input[placeholder]', v.form);
$.fn.isValid = function ( options ) {
// cache selector
var el = this;
// find the corrent field type
var type = ( el[0].getAttribute('type') ? el[0].getAttribute('type') : 'text' );
if ( type == 'textarea') { type = 'text' }; // IE7 bugfix which returns type of textarea not text
// return true or false
return ( o.pattern[type].test( el.val() ) && el.val() != el.attr('placeholder') && el.val() != "" ) ;
};
$.fn.classy = function ( options ) {
var el = this;
if ( options === false ) {
el.removeClass(o.classes.valid) // remove the valid class
el.addClass(o.classes.error) // add the error class
} else if ( options === true ) {
el.removeClass(o.classes.error) // remove the error class
el.addClass(o.classes.valid); // add the valid class
}
return this;
}
// Placeholder fallback
v.placeholders.each(function() {
if ( $(this).val() == "" ) {
$(this).val( $(this).attr('placeholder') ).addClass( o.classes.placeholder );
}
$(this).bind('blur focus', function(e) {
if ( e.type == "focus" && $(this).val() == $(this).attr('placeholder') ) {
$(this).val('').removeClass( o.classes.placeholder );
} else if ( e.type == "blur" && $(this).val() == "" ) {
$(this).val( $(this).attr('placeholder') ).addClass( o.classes.placeholder );
}
});
});
v.form.submit(function(e) {
// e.preventDefault();
// validation
var valid = true,
fields = v.requiredFields,
placeholders = v.placeholders;
$.each(fields, function(i, item) {
// validation 2.0
// run once on submit
if ( $(item).isValid() ) {
$(item).classy(true);
} else {
$(item).classy(false);
errorMessage($(item));
valid = false;
}
// after submitting once add live as-you-type validation
$(item).bind('keyup focus blur', function(e) {
// if there is a placeholder present do some magic
if ( e.type == 'focus' && $(this).val() == this.getAttribute('placeholder') && $(this).attr('placeholder') !== undefined ) {
$(this).val('').removeClass( o.classes.placeholder );
} else if ( e.type == 'blur' && $(this).val() == "" && $(this).attr('placeholder') !== undefined ) {
$(this).val( $(this).attr('placeholder') ).removeClass( o.classes.error ).addClass( o.classes.placeholder );
}
if ( e.type == 'keyup' ) {
if ( $(this).isValid() ) {
$(this).classy(true);
errorMessage($(item));
} else if ( $(this).val() != this.getAttribute('placeholder') ) {
$(this).classy(false)
errorMessage($(item));
valid = false;
}
}
});
}); // $.each loop
if ( !valid ) {
e.preventDefault();
} // if any fields are not filled out but required stop submit
}); // end submit
function errorMessage(el) {
var el = el,
id = el[0].getAttribute('id');
if ( el.data(o.errorMessageAttribute) !== undefined ) { // only run if there is a custom message
if ( el.hasClass(o.classes.error) && $('label[for='+ id +']').find('.validationMessage').length != 1 )
{
$('label[for='+ id +']').removeClass(o.classes.valid).addClass(o.classes.error)
.append('<span class=\"validationMessage\"></span>')
.find('.validationMessage')
.text('(' + el.data(o.errorMessageAttribute) + ')');
}
else if ( el.hasClass(o.classes.valid) )
{
$('label[for='+ id +']', v.form).removeClass(o.classes.error).addClass(o.classes.valid)
.find('.validationMessage')
.remove();
}
}
} // end errorMessage
// clear all classes and remove validation error messages when clearing the form.
v.clearBtn.click(function() {
$('input,label,textarea', v.form).removeClass(o.classes.error +' '+ o.classes.valid);
$('.validationMessage', v.form).remove();
});
});
}
});
})(jQuery);