4 Copyright (C) 2007 Apple Inc. All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 <title>SunSpider string-validate-input
</title>
29 <link rel=
"stylesheet" href=
"sunspider.css"></link>
33 <h3>string-validate-input
</h3>
39 letters
= new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
40 numbers
= new Array(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);
41 colors
= new Array("FF","CC","99","66","33","00");
49 // make up email address
50 for (var k
=0;k
<4000;k
++)
53 (k
%2)?email
=name
+"@mac.com":email
=name
+"(at)mac.com";
55 // validate the email address
56 var pattern
= /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*)\.[a-zA-Z]{2,3}$/;
58 if(pattern
.test(email
))
60 var r
= email
+ " appears to be a valid email address.";
65 r
= email
+ " does NOT appear to be a valid email address.";
71 for (var s
=0;s
<4000;s
++)
74 var zip
= makeNumber(4);
75 (s
%2)?zip
=zip
+"xyz":zip
=zip
.concat("7");
77 // validate the zip code
78 for (var i
= 0; i
< zip
.length
; i
++) {
79 var ch
= zip
.charAt(i
);
80 if (ch
< "0" || ch
> "9") {
82 r
= zip
+ " contains letters.";
86 if (zipGood
&& zip
.length
>5)
89 r
= zip
+ " is longer than five characters.";
94 r
= zip
+ " appears to be a valid ZIP code.";
103 for (var i
=0;i
<n
;i
++)
105 var l
= Math
.floor(26*Math
.random());
111 function makeNumber(n
)
114 for (var i
=0;i
<n
;i
++)
116 var l
= Math
.floor(9*Math
.random());
122 function addResult(r
)
124 endResult
+= "\n" + r
;