Coverage

100%
55
55
0

/Users/sebastiansandqvist/Documents/Sites & Projects/apps/modules/s-valid/index.js

100%
55
55
0
LineHitsSource
11'use strict';
2
31var valid = module.exports = {};
4
5
6// ----- affirmative
7// ---------------------------------------
81valid.affirmative = function(str) {
9
1027 var regex = /^(?:1|t(?:rue)?|y(?:es)?|on|ok(?:ay)?)$/;
11
1227 return regex.test(str.toLowerCase());
13
14};
15
16
17// ----- alphanumeric
18// ---------------------------------------
191valid.alphaNumeric = function(str) {
20
2117 var regex = /^[a-z0-9]+$/i;
22
2317 return regex.test(str.toLowerCase());
24
25};
26
27
28// ----- alphabetic
29// ---------------------------------------
301valid.alpha = function(str) {
31
3219 var regex = /^[a-z]+$/i;
33
3419 return regex.test(str.toLowerCase());
35
36};
37
38
39// ----- email
40// ---------------------------------------
411valid.email = function(str) {
42
4311 var regex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
44
4511 return regex.test(str.toLowerCase());
46
47};
48
49
50// ----- negatory
51// ---------------------------------------
521valid.negatory = function(str) {
53
5421 var regex = /^(?:1|f(?:alse)?|n(?:o)?|off)$/;
55
5621 return regex.test(str.toLowerCase());
57
58
59};
60
61
62// ----- numeric
63// ---------------------------------------
641valid.numeric = function(str) {
65
6614 str = str.toLowerCase();
67
6813 if (str === '') {
691 return false;
70 }
71
7212 return !isNaN(str); // http://stackoverflow.com/a/175787
73
74};
75
76
77// ----- value
78// ---------------------------------------
791valid.value = function(str) {
80
8127 str = str.toLowerCase().replace(',', '');
82
8326 var numbers = ['0','1','2','3','4','5','6','7','8','9', '-'];
84
85 // for $100 and #123, etc.
8626 if (numbers.indexOf(str[0]) === -1) {
8712 str = str.slice(1);
88 }
89
90
9126 return !isNaN(parseInt(str), 10);
92
93};
94
95
96// ----- url
97// ---------------------------------------
981valid.url = function(str) {
99
10023 var regex = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i;
101
10223 return regex.test(str.toLowerCase());
103
104};
105
106
107// ----- zip code
108// -- 1. input must be a string
109// -- 2. check number
110// -- 3. perform regex test
111// ---------------------------------------
1121valid.zipCode = function(str) {
113
114 // [1] throws here if not a string
11519 str = str.toLowerCase();
116
117 // [2] every zip code begins with a number, and none are < 00501
11818 var numeric = parseInt(str, 10);
119
12018 if (isNaN(numeric) || numeric < 501) {
1216 return false;
122 }
123
124 // [3]
12512 var regex = /^\d{5}(-\d{4})?$/;
126
12712 return regex.test(str);
128
129};
130
131
132// -------------- credit card methods --------------
133
134// ----- credit card check constructor
135// ---------------------------------------
1361function CreditCard(regex) {
137
13811 return function(str) {
139
14062 return regex.test(str.toLowerCase());
141
142 };
143
144}
145
146
147// ----- specific card regexp validation
148// ---------------------------------------
1491valid.card = {
150 visa: new CreditCard(/^4[0-9]{12}(?:[0-9]{3})?$/),
151 mastercard: new CreditCard(/^5[1-5][0-9]{14}$/),
152 amex: new CreditCard(/^3[47][0-9]{13}$/),
153 carteBlanche: new CreditCard(/^389[0-9]{11}$|^380[0-9]{11}$/),
154 dinersClub: new CreditCard(/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/),
155 discover: new CreditCard(/^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/),
156 jcb: new CreditCard(/^(?:2131|1800|35\d{3})\d{11}$/),
157 lasercard: new CreditCard(/^(6304|6706|6709|6771)[0-9]{12,15}$/),
158 maestro: new CreditCard(/^(5018|5020|5038|6304|6759|6761|6763|6799)[0-9]{8,19}$/),
159 solo: new CreditCard(/^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$|^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$/),
160 unionpay: new CreditCard(/^(62[0-9]{14,17})$/)
161};
162
163
164// ----- generic card validation
165// -- 1. input must be a string
166// -- 2. input length must be valid
167// -- 3. perform check based on number
168// ---------------------------------------
1691valid.creditCard = valid.card.generic = function(str) {
170
171 // [1] throws here if not a string
17260 str = str.toLowerCase();
173
174 // [2] must be valid card length
17558 if (str.length < 13 || str.length > 19) {
1764 return false;
177 }
178
179 // [3] method of testing credit cards from
180 // http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js
18154 var testNumber = 0;
182
18354 for (var i = str.length - 1, current, currentInteger = 0, even = false; i>=0; i--) {
184
185846 current = str.charAt(i);
186846 currentInteger = parseInt(current, 10);
187
188846 if (even && (currentInteger *= 2) > 9 ) {
189102 currentInteger -= 9;
190 }
191
192846 testNumber += currentInteger;
193846 even = !even;
194 }
195
19654 return (testNumber % 10) === 0;
197
198};