aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/validate/tests/validate.js
blob: 2fb272f01657484aea4232ea7334867d8a038215 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
if(!dojo._hasResource["dojox.validate.tests.validate"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.validate.tests.validate"] = true;
dojo.provide("dojox.validate.tests.validate"); 

dojo.require("dojox.validate._base");
dojo.require("dojox.validate.check");
dojo.require("dojox.validate.us");
dojo.require("dojox.validate.ca"); 
dojo.require("dojox.validate.web");
dojo.require("dojox.validate.isbn");

tests.register("dojox.validate.tests.validate",
	[{
		name: "isText",
		runTest: function(tests){
			tests.t(dojox.validate.isValidIsbn('0596007590')); //test string input
			tests.t(dojox.validate.isValidIsbn('0-596-00759-0')); //test string input with dashes
			tests.f(dojox.validate.isValidIsbn(596007590)); //test numerical input as well
			tests.t(dojox.validate.isValidIsbn("960-425-059-0")); 
			tests.t(dojox.validate.isValidIsbn(9604250590)); //test numerical input as well
			tests.t(dojox.validate.isValidIsbn('0-9752298-0-X')); // test string with X
			tests.t(dojox.validate.isValidIsbn('0-9752298-0-x')); 
			tests.t(dojox.validate.isValidIsbn('097522980x')); 
			tests.t(dojox.validate.isValidIsbn('097522980X')); 
			tests.f(dojox.validate.isValidIsbn(596007598)); //testing failures
			tests.f(dojox.validate.isValidIsbn('059-600759-X')); //testing failures
			tests.f(dojox.validate.isValidIsbn('059600')); // too short

			tests.t(dojox.validate.isValidIsbn('9780596007591'));
			tests.t(dojox.validate.isValidIsbn('978-0-596 00759-1'));
			tests.t(dojox.validate.isValidIsbn(9780596007591));
			tests.f(dojox.validate.isValidIsbn('978059600759X'));
			tests.f(dojox.validate.isValidIsbn('978-3250-596 00759-1 '));
			tests.f(dojox.validate.isValidIsbn('3250-596 00759 '));

			tests.t(dojox.validate.isText('            x'));
			tests.t(dojox.validate.isText('x             '));
			tests.t(dojox.validate.isText('        x     '));
			tests.f(dojox.validate.isText('   '));
			tests.f(dojox.validate.isText(''));
		
			// test lengths
			tests.t(dojox.validate.isText('123456', {length: 6} ));
			tests.f(dojox.validate.isText('1234567', {length: 6} ));
			tests.t(dojox.validate.isText('1234567', {minlength: 6} ));
			tests.t(dojox.validate.isText('123456', {minlength: 6} ));
			tests.f(dojox.validate.isText('12345', {minlength: 6} ));
			tests.f(dojox.validate.isText('1234567', {maxlength: 6} ));
			tests.t(dojox.validate.isText('123456', {maxlength: 6} ));
		}
	},
	{
		name: "isIpAddress",
		runTest: function(tests){
			tests.t(dojox.validate.isIpAddress('24.17.155.40'));
			tests.f(dojox.validate.isIpAddress('024.17.155.040'));       
			tests.t(dojox.validate.isIpAddress('255.255.255.255'));       
			tests.f(dojox.validate.isIpAddress('256.255.255.255'));       
			tests.f(dojox.validate.isIpAddress('255.256.255.255'));       
			tests.f(dojox.validate.isIpAddress('255.255.256.255'));       
			tests.f(dojox.validate.isIpAddress('255.255.255.256'));       

			// test dotted hex       
			tests.t(dojox.validate.isIpAddress('0x18.0x11.0x9b.0x28'));       
			tests.f(dojox.validate.isIpAddress('0x18.0x11.0x9b.0x28', {allowDottedHex: false}) );       
			tests.t(dojox.validate.isIpAddress('0x18.0x000000011.0x9b.0x28'));       
			tests.t(dojox.validate.isIpAddress('0xff.0xff.0xff.0xff'));       
			tests.f(dojox.validate.isIpAddress('0x100.0xff.0xff.0xff'));       

			// test dotted octal       
			tests.t(dojox.validate.isIpAddress('0030.0021.0233.0050'));       
			tests.f(dojox.validate.isIpAddress('0030.0021.0233.0050', {allowDottedOctal: false}) );
			tests.t(dojox.validate.isIpAddress('0030.0000021.0233.00000050'));       
			tests.t(dojox.validate.isIpAddress('0377.0377.0377.0377'));       
			tests.f(dojox.validate.isIpAddress('0400.0377.0377.0377'));       
			tests.f(dojox.validate.isIpAddress('0377.0378.0377.0377'));       
			tests.f(dojox.validate.isIpAddress('0377.0377.0380.0377'));       
			tests.f(dojox.validate.isIpAddress('0377.0377.0377.377'));       
		
			// test decimal       
			tests.t(dojox.validate.isIpAddress('3482223595'));       
			tests.t(dojox.validate.isIpAddress('0'));       
			tests.t(dojox.validate.isIpAddress('4294967295'));       
			tests.f(dojox.validate.isIpAddress('4294967296'));       
			tests.f(dojox.validate.isIpAddress('3482223595', {allowDecimal: false}));       
		
			// test hex       
			tests.t(dojox.validate.isIpAddress('0xCF8E83EB'));       
			tests.t(dojox.validate.isIpAddress('0x0'));       
			tests.t(dojox.validate.isIpAddress('0x00ffffffff'));       
			tests.f(dojox.validate.isIpAddress('0x100000000'));
			tests.f(dojox.validate.isIpAddress('0xCF8E83EB', {allowHex: false}));       
			
			// IPv6       
			tests.t(dojox.validate.isIpAddress('fedc:BA98:7654:3210:FEDC:BA98:7654:3210'));       
			tests.t(dojox.validate.isIpAddress('1080:0:0:0:8:800:200C:417A'));
			tests.f(dojox.validate.isIpAddress('1080:0:0:0:8:800:200C:417A', {allowIPv6: false}));
		
			// Hybrid of IPv6 and IPv4
			tests.t(dojox.validate.isIpAddress('0:0:0:0:0:0:13.1.68.3'));
			tests.t(dojox.validate.isIpAddress('0:0:0:0:0:FFFF:129.144.52.38'));
			tests.f(dojox.validate.isIpAddress('0:0:0:0:0:FFFF:129.144.52.38', {allowHybrid: false}));
			
		}
	},
	{
		name: "isUrlTest",
		runTest: function(tests){ 
			
			tests.t(dojox.validate.isUrl('www.yahoo.com'));
			tests.t(dojox.validate.isUrl('http://www.yahoo.com'));
			tests.t(dojox.validate.isUrl('https://www.yahoo.com'));
			tests.f(dojox.validate.isUrl('http://.yahoo.com'));
			tests.f(dojox.validate.isUrl('http://www.-yahoo.com'));
			tests.f(dojox.validate.isUrl('http://www.yahoo-.com'));
			tests.t(dojox.validate.isUrl('http://y-a---h-o-o.com'));
			tests.t(dojox.validate.isUrl('http://www.y.com'));
			tests.t(dojox.validate.isUrl('http://www.yahoo.museum'));
			tests.t(dojox.validate.isUrl('http://www.yahoo.co.uk'));
			tests.f(dojox.validate.isUrl('http://www.micro$oft.com'));
		
			tests.t(dojox.validate.isUrl('http://www.y.museum:8080'));
			tests.t(dojox.validate.isUrl('http://12.24.36.128:8080'));
			tests.f(dojox.validate.isUrl('http://12.24.36.128:8080', {allowIP: false} ));
			tests.t(dojox.validate.isUrl('www.y.museum:8080'));
			tests.f(dojox.validate.isUrl('www.y.museum:8080', {scheme: true} ));
			tests.t(dojox.validate.isUrl('localhost:8080', {allowLocal: true} ));
			tests.f(dojox.validate.isUrl('localhost:8080', {} ));
			tests.t(dojox.validate.isUrl('http://www.yahoo.com/index.html?a=12&b=hello%20world#anchor'));
			tests.f(dojox.validate.isUrl('http://www.yahoo.xyz'));
			tests.t(dojox.validate.isUrl('http://www.yahoo.com/index.html#anchor'));
			tests.t(dojox.validate.isUrl('http://cocoon.apache.org/2.1/'));
		}
	},
	{
		name: "isEmailAddress",
		runTest: function(tests) {
			tests.t(dojox.validate.isEmailAddress('x@yahoo.com'));
			tests.t(dojox.validate.isEmailAddress('x.y.z.w@yahoo.com'));
			tests.f(dojox.validate.isEmailAddress('x..y.z.w@yahoo.com'));
			tests.f(dojox.validate.isEmailAddress('x.@yahoo.com'));
			tests.t(dojox.validate.isEmailAddress('x@z.com'));
			tests.f(dojox.validate.isEmailAddress('x@yahoo.x'));
			tests.t(dojox.validate.isEmailAddress('x@yahoo.museum'));
			tests.t(dojox.validate.isEmailAddress("o'mally@yahoo.com"));
			tests.f(dojox.validate.isEmailAddress("'mally@yahoo.com"));
			tests.t(dojox.validate.isEmailAddress("fred&barney@stonehenge.com"));
			tests.f(dojox.validate.isEmailAddress("fred&&barney@stonehenge.com"));
		
			// local addresses
			tests.t(dojox.validate.isEmailAddress("fred&barney@localhost", {allowLocal: true} ));
			tests.f(dojox.validate.isEmailAddress("fred&barney@localhost"));
		
			// addresses with cruft
			tests.t(dojox.validate.isEmailAddress("mailto:fred&barney@stonehenge.com", {allowCruft: true} ));
			tests.t(dojox.validate.isEmailAddress("<fred&barney@stonehenge.com>", {allowCruft: true} ));
			tests.f(dojox.validate.isEmailAddress("mailto:fred&barney@stonehenge.com"));
			tests.f(dojox.validate.isEmailAddress("<fred&barney@stonehenge.com>"));
		
			// local addresses with cruft
			tests.t(dojox.validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowLocal: true, allowCruft: true} ));
			tests.f(dojox.validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowCruft: true} ));
			tests.f(dojox.validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowLocal: true} ));
		}
	},
	{
		name: "isEmailsAddressList",
		runTest: function(tests) {
			tests.t(dojox.validate.isEmailAddressList(
				"x@yahoo.com \n x.y.z.w@yahoo.com ; o'mally@yahoo.com , fred&barney@stonehenge.com \n" )
			);
			tests.t(dojox.validate.isEmailAddressList(
				"x@yahoo.com \n x.y.z.w@localhost \n o'mally@yahoo.com \n fred&barney@localhost", 
				{allowLocal: true} )
			);
			tests.f(dojox.validate.isEmailAddressList(
				"x@yahoo.com; x.y.z.w@localhost; o'mally@yahoo.com; fred&barney@localhost", {listSeparator: ";"} )
			);
			tests.t(dojox.validate.isEmailAddressList(
					"mailto:x@yahoo.com; <x.y.z.w@yahoo.com>; <mailto:o'mally@yahoo.com>; fred&barney@stonehenge.com", 
					{allowCruft: true, listSeparator: ";"} )
			);
			tests.f(dojox.validate.isEmailAddressList(
					"mailto:x@yahoo.com; <x.y.z.w@yahoo.com>; <mailto:o'mally@yahoo.com>; fred&barney@stonehenge.com", 
					{listSeparator: ";"} )
			);
			tests.t(dojox.validate.isEmailAddressList(
					"mailto:x@yahoo.com; <x.y.z.w@localhost>; <mailto:o'mally@localhost>; fred&barney@localhost", 
					{allowLocal: true, allowCruft: true, listSeparator: ";"} )
			);
		}
	},
	{
		name: "getEmailAddressList",
		runTest: function(tests) {
			var list = "x@yahoo.com \n x.y.z.w@yahoo.com ; o'mally@yahoo.com , fred&barney@stonehenge.com";
			tests.is(4, dojox.validate.getEmailAddressList(list).length);

			var localhostList = "x@yahoo.com; x.y.z.w@localhost; o'mally@yahoo.com; fred&barney@localhost";
			tests.is(0, dojox.validate.getEmailAddressList(localhostList).length);
			tests.is(4, dojox.validate.getEmailAddressList(localhostList, {allowLocal: true} ).length);
		}
	},
	{
		name: "isInRangeInt",
		runTest: function(tests) {
			// test integers
			tests.f(dojox.validate.isInRange( '0', {min: 1, max: 100} ));
			tests.t(dojox.validate.isInRange( '1', {min: 1, max: 100} ));
			tests.f(dojox.validate.isInRange( '-50', {min: 1, max: 100} ));
			tests.t(dojox.validate.isInRange( '+50', {min: 1, max: 100} ));
			tests.t(dojox.validate.isInRange( '100', {min: 1, max: 100} ));
			tests.f(dojox.validate.isInRange( '101', {min: 1, max: 100} ));
		}
	},
	{
		name:"isInRangeReal",
		runTest: function(tests){
	
			tests.f(dojox.validate.isInRange( '0.9', {min: 1.0, max: 10.0} ));
			tests.t(dojox.validate.isInRange( '1.0', {min: 1.0, max: 10.0} ));
			tests.f(dojox.validate.isInRange( '-5.0', {min: 1.0, max: 10.0} ));
			tests.t(dojox.validate.isInRange( '+5.50', {min: 1.0, max: 10.0} ));
			tests.t(dojox.validate.isInRange( '10.0', {min: 1.0, max: 10.0} ));
			tests.f(dojox.validate.isInRange( '10.1', {min: 1.0, max: 10.0} ));
			tests.f(dojox.validate.isInRange( '5.566e28', {min: 5.567e28, max: 6.000e28} ));
			tests.t(dojox.validate.isInRange( '5.7e28', {min: 5.567e28, max: 6.000e28} ));
			tests.f(dojox.validate.isInRange( '6.00000001e28', {min: 5.567e28, max: 6.000e28} ));
			tests.f(dojox.validate.isInRange( '10.000.000,12345e-5', {decimal: ",", max: 10000000.1e-5} ));
			tests.f(dojox.validate.isInRange( '10.000.000,12345e-5', {decimal: ",", min: 10000000.2e-5} ));
			tests.t(dojox.validate.isInRange('1,500,000', {separator: ',', min: 0}));
			tests.f(dojox.validate.isInRange('1,500,000', {separator: ',', min: 1000, max: 20000}));
		}
	},
	{
			
		name:"isInRangeCurrency",
		runTest: function(test){
			
			tests.f(dojox.validate.isInRange('\u20AC123,456,789', {max: 123456788, symbol: '\u20AC'} ));
			tests.f(dojox.validate.isInRange('\u20AC123,456,789', { min: 123456790, symbol: '\u20AC'} ));
			tests.f(dojox.validate.isInRange('$123,456,789.07', { max: 123456789.06} ));
			tests.f(dojox.validate.isInRange('$123,456,789.07', { min: 123456789.08} ));
			tests.f(dojox.validate.isInRange('123.456.789,00 \u20AC',  {max: 123456788, decimal: ",", symbol: '\u20AC'} ));
			tests.f(dojox.validate.isInRange('123.456.789,00 \u20AC',  {min: 123456790, decimal: ",", symbol: '\u20AC'} ));
			tests.f(dojox.validate.isInRange('- T123 456 789-00', {decimal: "-", min:0} ));
			tests.t(dojox.validate.isInRange('\u20AC123,456,789', { max: 123456790, symbol: '\u20AC'} ));
			tests.t(dojox.validate.isInRange('$123,456,789.07', { min: 123456789.06} ));
			// test non number
			//tests.f("test25", dojox.validate.isInRange( 'a'));
		}
	},
	{	
		name: "isUsPhoneNumber",
		runTest: function(tests) {
			tests.t(dojox.validate.us.isPhoneNumber('(111) 111-1111'));
			tests.t(dojox.validate.us.isPhoneNumber('(111) 111 1111'));
			tests.t(dojox.validate.us.isPhoneNumber('111 111 1111'));
			tests.t(dojox.validate.us.isPhoneNumber('111.111.1111'));
			tests.t(dojox.validate.us.isPhoneNumber('111-111-1111'));
			tests.t(dojox.validate.us.isPhoneNumber('111/111-1111'));
			tests.f(dojox.validate.us.isPhoneNumber('111 111-1111'));
			tests.f(dojox.validate.us.isPhoneNumber('111-1111'));
			tests.f(dojox.validate.us.isPhoneNumber('(111)-111-1111'));
		
			// test extensions
			tests.t(dojox.validate.us.isPhoneNumber('111-111-1111 x1'));
			tests.t(dojox.validate.us.isPhoneNumber('111-111-1111 x12'));
			tests.t(dojox.validate.us.isPhoneNumber('111-111-1111 x1234'));
		}
	},
	{
		name: "isUsSocialSecurityNumber",
		runTest: function(tests) {
			tests.t(dojox.validate.us.isSocialSecurityNumber('123-45-6789'));
			tests.t(dojox.validate.us.isSocialSecurityNumber('123 45 6789'));
			tests.t(dojox.validate.us.isSocialSecurityNumber('123456789'));
			tests.f(dojox.validate.us.isSocialSecurityNumber('123-45 6789'));
			tests.f(dojox.validate.us.isSocialSecurityNumber('12345 6789'));
			tests.f(dojox.validate.us.isSocialSecurityNumber('123-456789'));
		}
	},
	{
		name:"isUsZipCode",
		runTest: function(tests) {
			tests.t(dojox.validate.us.isZipCode('12345-6789'));
			tests.t(dojox.validate.us.isZipCode('12345 6789'));
			tests.t(dojox.validate.us.isZipCode('123456789'));
			tests.t(dojox.validate.us.isZipCode('12345'));
		}
	},
	{
		name:"isCaZipCode",
		runTest: function(tests) {
			tests.t(dojox.validate.ca.isPostalCode('A1Z 3F3'));
			tests.f(dojox.validate.ca.isPostalCode('1AZ 3F3'));
			tests.t(dojox.validate.ca.isPostalCode('a1z 3f3'));
			tests.f(dojox.validate.ca.isPostalCode('xxxxxx'));
			tests.t(dojox.validate.ca.isPostalCode('A1Z3F3')); 
			
		}
	},
	{
		name:"isUsState",
		runTest: function(tests) {
			tests.t(dojox.validate.us.isState('CA'));
			tests.t(dojox.validate.us.isState('ne'));
			tests.t(dojox.validate.us.isState('PR'));
			tests.f(dojox.validate.us.isState('PR', {allowTerritories: false} ));
			tests.t(dojox.validate.us.isState('AA'));
			tests.f(dojox.validate.us.isState('AA', {allowMilitary: false} ));
		}
	}
]);

}