0
# Pattern Matching
1
2
Regular expression-based validation for common data formats including emails, URLs, phone numbers, and postal codes.
3
4
## Capabilities
5
6
### Email Validation
7
8
Checks if the given value matches email format.
9
10
```javascript { .api }
11
/**
12
* Checks if the given value matches email regexp
13
* @param value - Value to check
14
* @returns True if value matches email format
15
* Interfaces: not, all, any
16
*/
17
function email(value: any): boolean;
18
```
19
20
**Usage Example:**
21
22
```javascript
23
is.email('test@test.com'); // true
24
is.email('foo'); // false
25
is.not.email('foo'); // true
26
is.all.email('test@test.com', 'foo'); // false
27
is.any.email('test@test.com', 'foo'); // true
28
```
29
30
### URL Validation
31
32
Checks if the given value matches URL format.
33
34
```javascript { .api }
35
/**
36
* Checks if the given value matches url regexp
37
* @param value - Value to check
38
* @returns True if value matches URL format
39
* Interfaces: not, all, any
40
*/
41
function url(value: any): boolean;
42
```
43
44
**Usage Example:**
45
46
```javascript
47
is.url('http://www.test.com'); // true
48
is.url('foo'); // false
49
is.not.url(true); // true
50
is.all.url('http://www.test.com', 'foo'); // false
51
is.any.url('http://www.test.com', true); // true
52
```
53
54
### Credit Card Validation
55
56
Checks if the given value matches credit card number format.
57
58
```javascript { .api }
59
/**
60
* Checks if the given value matches credit card regexp
61
* @param value - Value to check
62
* @returns True if value matches credit card format
63
* Interfaces: not, all, any
64
*/
65
function creditCard(value: any): boolean;
66
```
67
68
**Usage Example:**
69
70
```javascript
71
is.creditCard(378282246310005); // true
72
is.creditCard(123); // false
73
is.not.creditCard(123); // true
74
is.all.creditCard(378282246310005, 123); // false
75
is.any.creditCard(378282246310005, 123); // true
76
```
77
78
### Alphanumeric Validation
79
80
Checks if the given value matches alphanumeric format.
81
82
```javascript { .api }
83
/**
84
* Checks if the given value matches alpha numeric regexp
85
* @param value - Value to check
86
* @returns True if value contains only letters and numbers
87
* Interfaces: not, all, any
88
*/
89
function alphaNumeric(value: any): boolean;
90
```
91
92
**Usage Example:**
93
94
```javascript
95
is.alphaNumeric('alphaNu3er1k'); // true
96
is.alphaNumeric('*?'); // false
97
is.not.alphaNumeric('*?'); // true
98
is.all.alphaNumeric('alphaNu3er1k', '*?'); // false
99
is.any.alphaNumeric('alphaNu3er1k', '*?'); // true
100
```
101
102
### Date String Validation
103
104
Checks if the given value matches date string format (m/d/yy and mm/dd/yyyy).
105
106
```javascript { .api }
107
/**
108
* Checks if the given value matches date string regexp
109
* @param value - Value to check
110
* @returns True if value matches date string format
111
* Interfaces: not, all, any
112
*/
113
function dateString(value: any): boolean;
114
```
115
116
**Usage Example:**
117
118
```javascript
119
is.dateString('11/11/2011'); // true
120
is.dateString('10-21-2012'); // true
121
is.dateString('90/11/2011'); // false
122
is.not.dateString('90/11/2011'); // true
123
is.all.dateString('11/11/2011', '90/11/2011'); // false
124
is.any.dateString('11-11-2011', '90/11/2011'); // true
125
```
126
127
### Time String Validation
128
129
Checks if the given value matches time string format (HH:MM:SS).
130
131
```javascript { .api }
132
/**
133
* Checks if the given value matches time string regexp
134
* @param value - Value to check
135
* @returns True if value matches time format
136
* Interfaces: not, all, any
137
*/
138
function timeString(value: any): boolean;
139
```
140
141
**Usage Example:**
142
143
```javascript
144
is.timeString('13:45:30'); // true
145
is.timeString('90:90:90'); // false
146
is.not.timeString('90:90:90'); // true
147
is.all.timeString('13:45:30', '90:90:90'); // false
148
is.any.timeString('13:45:30', '90:90:90'); // true
149
```
150
151
### IP Address Validation
152
153
Checks if the given value matches IP address format (IPv4 or IPv6).
154
155
```javascript { .api }
156
/**
157
* Checks if the given value matches ip regexp
158
* @param value - Value to check
159
* @returns True if value matches IPv4 or IPv6 format
160
* Interfaces: not, all, any
161
*/
162
function ip(value: any): boolean;
163
164
/**
165
* Checks if the given value matches ipv4 regexp
166
* @param value - Value to check
167
* @returns True if value matches IPv4 format
168
* Interfaces: not, all, any
169
*/
170
function ipv4(value: any): boolean;
171
172
/**
173
* Checks if the given value matches ipv6 regexp
174
* @param value - Value to check
175
* @returns True if value matches IPv6 format
176
* Interfaces: not, all, any
177
*/
178
function ipv6(value: any): boolean;
179
```
180
181
**Usage Example:**
182
183
```javascript
184
is.ip('198.156.23.5'); // true
185
is.ip('2001:DB8:0:0:1::1'); // true
186
is.ipv4('198.12.3.142'); // true
187
is.ipv6('2001:DB8:0:0:1::1'); // true
188
is.not.ip('8:::::::7'); // true
189
```
190
191
### Phone Number Validation
192
193
Checks if the given value matches phone number formats.
194
195
```javascript { .api }
196
/**
197
* Checks if the given value matches North American numbering plan phone regexp
198
* @param value - Value to check
199
* @returns True if value matches NANP phone format
200
* Interfaces: not, all, any
201
*/
202
function nanpPhone(value: any): boolean;
203
204
/**
205
* Checks if the given value matches extensible provisioning protocol phone regexp
206
* @param value - Value to check
207
* @returns True if value matches EPP phone format
208
* Interfaces: not, all, any
209
*/
210
function eppPhone(value: any): boolean;
211
```
212
213
**Usage Example:**
214
215
```javascript
216
is.nanpPhone('609-555-0175'); // true
217
is.eppPhone('+90.2322456789'); // true
218
is.nanpPhone('123'); // false
219
is.not.nanpPhone('123'); // true
220
```
221
222
### Postal Code Validation
223
224
Checks if the given value matches postal code formats for different countries.
225
226
```javascript { .api }
227
/**
228
* Checks if the given value matches US zip code regexp
229
* @param value - Value to check
230
* @returns True if value matches US ZIP code format
231
* Interfaces: not, all, any
232
*/
233
function usZipCode(value: any): boolean;
234
235
/**
236
* Checks if the given value matches Canada postal code regexp
237
* @param value - Value to check
238
* @returns True if value matches Canadian postal code format
239
* Interfaces: not, all, any
240
*/
241
function caPostalCode(value: any): boolean;
242
243
/**
244
* Checks if the given value matches UK post code regexp
245
* @param value - Value to check
246
* @returns True if value matches UK postal code format
247
* Interfaces: not, all, any
248
*/
249
function ukPostCode(value: any): boolean;
250
```
251
252
**Usage Example:**
253
254
```javascript
255
is.usZipCode('02201-1020'); // true
256
is.caPostalCode('L8V3Y1'); // true
257
is.caPostalCode('L8V 3Y1'); // true
258
is.ukPostCode('B184BJ'); // true
259
```
260
261
### Social Security Number Validation
262
263
Checks if the given value matches US Social Security Number format.
264
265
```javascript { .api }
266
/**
267
* Checks if the given value matches social security number regexp
268
* @param value - Value to check
269
* @returns True if value matches SSN format
270
* Interfaces: not, all, any
271
*/
272
function socialSecurityNumber(value: any): boolean;
273
```
274
275
**Usage Example:**
276
277
```javascript
278
is.socialSecurityNumber('017-90-7890'); // true
279
is.socialSecurityNumber('017907890'); // true
280
is.socialSecurityNumber('123'); // false
281
is.not.socialSecurityNumber('123'); // true
282
```
283
284
### Hexadecimal and Color Validation
285
286
Checks if the given value matches hexadecimal or color formats.
287
288
```javascript { .api }
289
/**
290
* Checks if the given value matches hexadecimal regexp
291
* @param value - Value to check
292
* @returns True if value is hexadecimal
293
* Interfaces: not, all, any
294
*/
295
function hexadecimal(value: any): boolean;
296
297
/**
298
* Checks if the given value matches hexcolor regexp
299
* @param value - Value to check
300
* @returns True if value is hex color code
301
* Interfaces: not, all, any
302
*/
303
function hexColor(value: any): boolean;
304
```
305
306
**Usage Example:**
307
308
```javascript
309
is.hexadecimal('f0f0f0'); // true
310
is.hexadecimal('0xf0f0f0'); // true
311
is.hexColor('#333'); // true
312
is.hexColor('#3333'); // false
313
is.all.hexColor('fff', 'f50'); // true
314
```
315
316
### Affirmative Response Validation
317
318
Checks if the given value matches affirmative response patterns.
319
320
```javascript { .api }
321
/**
322
* Checks if the given value matches affirmative regexp
323
* @param value - Value to check
324
* @returns True if value is affirmative (yes, true, ok, etc.)
325
* Interfaces: not, all, any
326
*/
327
function affirmative(value: any): boolean;
328
```
329
330
**Usage Example:**
331
332
```javascript
333
is.affirmative('yes'); // true
334
is.affirmative('true'); // true
335
is.affirmative('ok'); // true
336
is.affirmative('no'); // false
337
is.not.affirmative('no'); // true
338
is.all.affirmative(['yes', 'y', 'true', 't', 'ok', 'okay']); // true
339
```