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-Za-z0-9]+$/;
22
2317 return regex.test(str.toLowerCase());
24
25};
26
27
28// ----- alphabetic
29// ---------------------------------------
301valid.alpha = function(str) {
31
3219 var regex = /^[A-Za-z]+$/;
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// ---------------------------------------
1091valid.zipCode = function(str) {
110
11119 str = str.toLowerCase();
112
11318 var numeric = parseInt(str, 10);
114
11518 if (isNaN(numeric) || numeric < 501) {
1166 return false;
117 }
118
11912 var regex = /^\d{5}(-\d{4})?$/;
120
12112 return regex.test(str);
122
123};
124
125
126// -------------- credit card methods --------------
127
128// ----- credit card check constructor
129// ---------------------------------------
1301function CreditCard(regex) {
131
13211 return function(str) {
133
13462 return regex.test(str.toLowerCase());
135
136 };
137
138}
139
140
141// ----- specific card regexp validation
142// ---------------------------------------
1431valid.card = {
144 visa: new CreditCard(/^4[0-9]{12}(?:[0-9]{3})?$/),
145 mastercard: new CreditCard(/^5[1-5][0-9]{14}$/),
146 amex: new CreditCard(/^3[47][0-9]{13}$/),
147 carteBlanche: new CreditCard(/^389[0-9]{11}$|^380[0-9]{11}$/),
148 dinersClub: new CreditCard(/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/),
149 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})$/),
150 jcb: new CreditCard(/^(?:2131|1800|35\d{3})\d{11}$/),
151 lasercard: new CreditCard(/^(6304|6706|6709|6771)[0-9]{12,15}$/),
152 maestro: new CreditCard(/^(5018|5020|5038|6304|6759|6761|6763|6799)[0-9]{8,19}$/),
153 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}$/),
154 unionpay: new CreditCard(/^(62[0-9]{14,17})$/)
155};
156
157
158// ----- generic card validation
159// -- 1. input must be number
160// -- 2. input length must be valid
161// -- 3. return true if any specific test passes
162// -- 4. perform complex check based on number
163// ---------------------------------------
1641valid.creditCard = valid.card.generic = function(str) {
165
166 // throws here if not a string.
16760 str = str.toLowerCase();
168
169 // must be valid card length
17058 if (str.length < 13 || str.length > 19) {
1714 return false;
172 }
173
174 // method of testing credit cards from
175 // http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js
17654 var testNumber = 0;
177
17854 for (var i = str.length - 1, current, currentInteger = 0, even = false; i>=0; i--) {
179
180846 current = str.charAt(i);
181846 currentInteger = parseInt(current, 10);
182
183846 if (even && (currentInteger *= 2) > 9 ) {
184102 currentInteger -= 9;
185 }
186
187846 testNumber += currentInteger;
188846 even = !even;
189 }
190
19154 return (testNumber % 10) === 0;
192
193};