-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis.dev.js
116 lines (85 loc) · 3.17 KB
/
is.dev.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
// ============================================================================
// is.js 1.0.0
// ============================================================================
// > http://wyattkirby.com/is
// > (c) 2013 Wyatt Kirby
// > is.js may be freely distributed under the MIT License.
// ============================================================================
(function() {
'use strict';
// Setup
// ------------------------------------------------------------------------
var root = this,
is = is || {};
root.is = is;
is.VERSION = '1.0.0';
// Regex Library
// ------------------------------------------------------------------------
var emailRegex = new RegExp("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$"), // HTML5 Compliant
urlRegex = new RegExp("^(?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’])$"),
dateRegex = new RegExp("^(([1-9])|(0[1-9])|(1[0-2]))\\/((0[1-9])|([1-31]))\\/((\\d{2})|(\\d{4}))$"), // Matches: 01/01/2001, 1/1/2001, 01/1/01
phoneRegex = new RegExp("^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$"); // Matches: (111) 222-3333, 1112223333, 111-222-3333
// Utilities
// ------------------------------------------------------------------------
var compare = function(input, regex) {
if (!input || !regex) {
return false;
}
return regex.test(input);
};
// Comparators
// ------------------------------------------------------------------------
// Format Checking
is.email = function(input) {
return compare(input, emailRegex);
};
is.url = function(input) {
return compare(input, urlRegex);
};
is.date = function(input) {
return compare(input, dateRegex);
};
is.phone = function(input) {
return compare(input, phoneRegex);
};
// Type Checking
is.string = function(input) {
return typeof input === "string" || input instanceof String;
};
is.number = function(input) {
return typeof input === "number" || input instanceof Number;
};
is.bool = function(input) {
return input === !!input || input instanceof Boolean;
};
is.array = function(input) {
return typeof input === "array" || input instanceof Array;
};
// Number Checking
is.wholeNum = function(input) {
var number = parseInt(input, 10);
return (is.number(number) && !isNaN(number) && number % 1 === 0) ? true : false;
};
is.even = function(input) {
var number = parseInt(input, 10);
return (is.number(number) && number % 2 === 0) ? true : false;
};
is.odd = function(input) {
return !is.even(input);
};
// Public Setters
// ------------------------------------------------------------------------
// Setting RegEx done as strings, which require double escaping.
is.setEmailRegex = function(newRegEx) {
emailRegex = new RegExp(newRegEx);
};
is.setURLRegex = function(newRegEx) {
urlRegex = new RegExp(newRegEx);
};
is.setDateRegex = function(newRegEx) {
dateRegex = new RegExp(newRegEx);
};
is.setPhoneRegex = function(newRegEx) {
phoneRegex = new RegExp(newRegEx);
};
}).call(this);