forked from soitgoes/FormWarden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formwarden.js
194 lines (174 loc) · 5.76 KB
/
formwarden.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
186
187
188
189
190
191
192
193
194
(function (definition) {
// This file will function properly as a <script> tag, or a module
// using CommonJS and NodeJS or RequireJS module formats. In
// Common/Node/RequireJS, the module exports the Q API and when
// executed as a simple <script>, it creates a Q global instead.
// RequireJS
if (typeof define === "function") {
define(definition);
// CommonJS
} else if (typeof exports === "object") {
definition(require, exports);
// <script>
} else {
definition(void 0, fw = {});
}
})(function (serverSideRequire, exports) {
"use strict";
var hasOwnProperty = Object.prototype.hasOwnProperty;
var nativeForEach = Array.prototype.forEach;
var slice = Array.prototype.slice;
/* Taken from Underscore.js */
// Establish the object that gets returned to break out of a loop iteration.
var breaker = {};
var each = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
}
};
// Extend a given object with all the properties in passed-in object(s).
var extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (source[prop] !== void 0) obj[prop] = source[prop];
}
});
return obj;
};
/* End Taken from Underscore.js */
var defaultValidators = {
required: function(value){
if (value === undefined || value === null) {
return true;
}
return value.length || value !== "";
},
email: function(value){
if (value){
return value.match(/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i);
}
},
ssn: function(value){
if(value){
return value.match(/\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b/);
}
},
date: function(value){
if(value){
return value.match(/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}/);
}
},
iso8601: function(value){
if(value){
return value.match(/^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$/);
}
},
maxLength: function(value, form, validation){
if(value){
return value.length <= validation.length;
}
},
minLength: function(value,form, validation){
if(value){
return value.length >= validation.length;
}
}
};
var isInvalidField = function (form, key, fieldOptions, validators, conventionBased){
if (!fieldOptions){
return true;
}
var value = form[key];
var checkVisibility = fieldOptions.push ? false : true;
//if there is not a visibility function or it is true then execute the validation
var validations = checkVisibility ? fieldOptions.validations : fieldOptions;
for (var i = 0; i < validations.length; i++)
{
var validation = validations[i];
if (validation.isValid.test){ //regex
var pattern = validation.isValid;
if (!pattern.test(value)){
return validation.message || key + " is invalid";
}
}else if (typeof validation.isValid == "function" ){
if (!validation.isValid(value, form)){
return validation.message || key + " is invalid";
}
}else{
var validFx = validators[validation.isValid];
if (validFx){
if (!validFx(value, form, validation)){
if (validation.invalid){
validation.invalid();
}
return validation.message || key + " is invalid";
}else{
if (validation.valid){
validation.valid();
}
}
}else{
console.log("Validation Function not found: " + validation.isValid)
}
}
}
return "";
}
var isVisibleField = function(form, fieldOptions){
if (fieldOptions && fieldOptions.isVisible && typeof fieldOptions.isVisible == "function" && !fieldOptions.isVisible(form)){
return false;
}
return true;
}
//if server side don't bother suppling fieldsEntered
exports.validateForm= function(form, validationOptions){
var fields = {};
var validForm = true;
var validators = extend(defaultValidators, validationOptions.validators || {});
each(validationOptions.fields, function(val, key) {
var visible = isVisibleField(form, val),
mesg = isInvalidField(form, key, val, validators, validationOptions.conventionBased),
valid = (!visible) || (visible && mesg === ""),
result;
validForm &= valid;
result = { visible: visible, error: mesg, valid: !!valid };
if (result) {
fields[key] = result;
}
});
each(form, function(val, key) {
// Form fields that have no validators are visible and valid
if (validationOptions.conventionBased){
//loop through all of the validators if any of them partial match then execute them and return
if (val !=""){
for (var vkey in validators){
if (key.indexOf(vkey) > -1){
var valid = !!validators[vkey](val, form);
var mesg = valid ? "" : validators[vkey].message || key + " is invalid";
fields[key] = {visible:true, error: mesg, valid: valid} ;
validForm &= valid;
}
}
}
}
if (!hasOwnProperty.call(fields, key)) {
fields[key] = { visible: true, error: "", valid: true };
}
});
return {
fields: fields,
validForm: !!validForm
};
}
});