-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.attest.js
More file actions
132 lines (132 loc) · 4.42 KB
/
Copy pathjquery.attest.js
File metadata and controls
132 lines (132 loc) · 4.42 KB
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
(function() {
var attest;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
attest = (function() {
attest.prototype.defaults = {
nodes: 'input, select, textarea',
ignored: '[type=submit], [type=image], [type=button], [type=reset], [type=hidden]',
submit: '[type=submit]',
validClass: 'valid',
errorClass: 'error',
requiredClass: 'required',
modifiers: 'i',
patterns: {
email: /^[a-z0-9_.%+\-]+@[0-9a-z.\-]+\.[a-z.]{2,6}$/i,
url: /[a-z][\-\.+a-z]*:\/\//i,
number: /^\d+$/
}
};
function attest(form, options) {
this.form = $(form);
this.options = $.extend(true, {}, this.defaults, options);
this.fields = this.form.find(this.options.nodes).not(this.options.ignored);
this.submit = this.form.find(this.options.submit);
this._bindings();
this.submit.bind('click', __bind(function(e) {
if (this.validate().length) {
return false;
}
}, this));
this.form.bind('submit', __bind(function(e) {
if (e.which === 13 && this.validate().length) {
return false;
}
}, this));
}
attest.prototype._bindings = function() {
return this.fields.bind('blur keyup keydown', __bind(function(e) {
var el, invalid;
el = $(e.currentTarget);
if (!(e.type === 'blur' || el.hasClass('error'))) {
return;
}
return invalid = this._isRequired(el) || this._isValid(el);
}, this));
};
attest.prototype._isRequired = function(el) {
var required, val;
required = el.attr('required');
val = el.val();
if (required && (!(val != null) || val.length === 0)) {
el.addClass(this.options.requiredClass);
return true;
} else {
el.removeClass(this.options.requiredClass);
}
return null;
};
attest.prototype._isValid = function(el) {
var match, pattern;
if (el.data('match')) {
match = this.form.find(el.data('match'));
if ((match != null) && $(match).val() !== el.val()) {
el.addClass(this.options.errorClass);
return true;
} else {
el.removeClass(this.options.errorClass);
}
}
pattern = el.attr('pattern') ? new RegExp(el.attr('pattern'), this.options.modifiers) : this.options.patterns[el.attr('type')] ? this.options.patterns[el.attr('type')] : void 0;
if (pattern) {
if (pattern.test(el.val())) {
el.removeClass(this.options.errorClass);
} else {
el.addClass(this.options.errorClass);
return true;
}
}
return null;
};
attest.prototype._option = function(key, value) {
if ($.isPlainObject(key)) {
return this.options = $.extend(true, this.options, key);
}
};
attest.prototype.validate = function() {
return this.fields.map(__bind(function(idx, field) {
var el, invalid;
el = $(field);
invalid = this._isRequired(el) || this._isValid(el);
if (invalid) {
el.addClass(this.options.errorClass);
}
if (invalid) {
return field;
} else {
return null;
}
}, this));
};
return attest;
})();
jQuery.fn.attest = function(options) {
var args, isMethodCall, returnValue;
isMethodCall = typeof options === "string";
args = Array.prototype.slice.call(arguments, 1);
returnValue = this;
this.each(function() {
var instance, methodValue;
instance = $.data(this, 'attest');
if (isMethodCall) {
if (!instance) {
return $.error("cannot call methods on attest prior to initialization; attempted to call method '" + options + "'");
}
if (!$.isFunction(instance[options]) || options.charAt(0) === "_") {
return $.error("no such method '" + options + "' for attest instance");
}
methodValue = instance[options].apply(instance, args);
if ((methodValue != null) && methodValue !== instance) {
returnValue = (methodValue && methodValue.jquery ? returnValue.pushStack(methodValue.get()) : methodValue);
return false;
}
} else {
if (instance) {
return instance._option(options || {});
} else {
return $.data(this, 'attest', new attest(this, options));
}
}
});
return returnValue;
};
}).call(this);