forked from Availity/textcomplies
/
jquery.textcomplies.js
226 lines (214 loc) · 8.09 KB
/
jquery.textcomplies.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*!
* TextComplies jQuery Plugin
*
* Copyright (c) 2011 Availity, LLC
* Licensed under the MIT license
*
* Author: Robert Corbin, Robbie Rhoden, Rob Warner
* Version: 1.0
*/
(function($) {
var methods = {
init : function(options) {
var settings = {
minLength : -1,
minLengthText : "Must have at least # character%",
maxLength : -1,
maxLengthText : "Must have no more than # character%",
numNumbers : -1,
numNumbersText : "Must have at least # number%",
numUppercaseLetters : -1,
numUppercaseLettersText : "Must have at least # capital letter%",
numLowercaseLetters : -1,
numLowercaseLettersText : "Must have at least # lowercase letter%",
numLetters : -1,
numLettersText : "Must have at least # letter%",
disallowed : null,
disallowedText : "Can't contain @",
matchField : null,
matchFieldText : "Text entry matches",
output : null,
onComplies : null,
onDefies : null
};
if (options) {
$.extend(settings, options);
}
return this.each(function() {
/**
* Get a pointer to the field to check
*/
var fieldForCompliance = $(this);
/**
* Test the text from the specified field for compliance with the configured options
* @param text
* @param options
*/
function complies(text, options) {
var results = {
minLength : false,
maxLength : false,
numNumbers : false,
numUppercaseLetters : false,
numLowercaseLetters : false,
numLetters : false,
disallowed : false,
matchField : false
};
results.minLength = options.minLength === -1 || (text.length >= options.minLength);
results.maxLength = options.maxLength === -1 || (text.length <= options.maxLength);
results.numNumbers = countComplies(options.numNumbers, text, /[0-9]/g);
results.numLetters = countComplies(options.numLetters, text, /[a-zA-Z]/g);
results.numUppercaseLetters = countComplies(options.numUppercaseLetters, text, /[A-Z]/g);
results.numLowercaseLetters = countComplies(options.numLowercaseLetters, text, /[a-z]/g);
results.disallowed = containsComplies(options.disallowed, text);
results.matchField = options.matchField != null && text === $(options.matchField).val();
return results;
}
/**
* Returns whether all of the properties on an object (but not its inherited properties)
* are true
* @param obj
*/
function allPropertiesTrue(obj) {
var allTrue = true;
for (var property in obj) {
if (obj.hasOwnProperty(property) && !obj[property]) {
allTrue = false;
break;
}
}
return allTrue;
}
/**
* Builds an item in the list
* @param option
* @param result
* @param message
*/
function buildItem(option, result, message) {
var item = null;
if (!(option === null || option === -1 || option.length === 0)) {
item = message.replace("#", option).replace("%", option === 1 ? "" : "s").replace("@", listWords(option));
}
return item === null ? '' : wrapItem(item, result);
}
function wrapItem(item, complies) {
return '<li class="' + (complies ? "complies" : "defies") + '">' + item + '</li>';
}
/**
* Returns whether the text has at least the specified number of matches for the regex
* @param option
* @param text
* @param regex
*/
function countComplies(option, text, regex) {
var passes = true;
if (option != -1) {
var matches = text.match(regex);
passes = (matches && (matches.length >= option));
}
return passes;
}
/**
* Returns whether the specified text contains the characters
* @param option
* @param text
*/
function containsComplies(option, text) {
var passes = true;
if (option != null) {
for (var i = 0, n = option.length; i < n; i++) {
if (text.indexOf(option[i]) != -1) {
passes = false;
break;
}
}
}
return passes;
}
/**
* Returns an HTML version of a CSV list of the words in the specified array,
* so that users can see a list of the specified words in a reader-friendly format.
* A space is converted to the text <space>
* @param arr
*/
function listWords(arr) {
var result = "";
for (var i = 0, n = arr.length; i < n; i++) {
result += arr[i];
if (i < (n - 1)) {
result += ",";
}
}
result = result.replace(/ /g, "<space>").replace(/,/g, ", ").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
return result;
}
/**
* Creates the compliance check function that runs the complies function and
* builds the html output
*/
var performComplianceCheck = function() {
var results = complies(fieldForCompliance.val(), settings);
if (settings.output) {
var html = "<ul>";
if (settings.minLength > -1 && settings.maxLength > -1) {
html += buildItem(1, results.minLength && results.maxLength, "Must have " + settings.minLength + " - " + settings.maxLength + " characters");
} else {
html += buildItem(settings.minLength, results.minLength, settings.minLengthText);
html += buildItem(settings.maxLength, results.maxLength, settings.maxLengthText);
}
html += buildItem(settings.numNumbers, results.numNumbers, settings.numNumbersText);
html += buildItem(settings.numLetters, results.numLetters, settings.numLettersText);
html += buildItem(settings.numUppercaseLetters, results.numUppercaseLetters, settings.numUppercaseLettersText);
html += buildItem(settings.numLowercaseLetters, results.numLowercaseLetters, settings.numLowercaseLettersText);
html += buildItem(settings.disallowed, results.disallowed, settings.disallowedText);
html += buildItem(settings.matchField, results.matchField, settings.matchFieldText);
html += "</ul>";
$(settings.output).html(html);
}
var allTrue = allPropertiesTrue(results);
if (allTrue && settings.onComplies != null) {
settings.onComplies();
} else if (!allTrue && settings.onDefies != null) {
settings.onDefies();
}
};
/**
* Sets up the plugin to run on each keypress
*/
$(this).bind('keyup.textComplies', performComplianceCheck);
/**
* If a matching field was specified, bind keypresses so that the matching
* compliance is updated as they type in the matching field
*/
if (options.matchField != null) {
$(options.matchField).bind('keyup.textComplies', performComplianceCheck);
}
});
},
destroy : function(options) {
return this.each(function() {
$(this).unbind('.textComplies');
var settings = {
matchField : null
};
if (options) {
$.extend(settings, options);
}
if (settings.matchField != null) {
$(settings.matchField).unbind('.textComplies');
}
});
}
};
$.fn.textComplies = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.textComplies');
}
};
})(jQuery);