string-validate-input.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <head>
  3. <!--
  4. Copyright (C) 2007 Apple Inc. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. -->
  25. <title>SunSpider string-validate-input</title>
  26. </head>
  27. <body>
  28. <h3>string-validate-input</h3>
  29. <div id="console">
  30. </div>
  31. <script>
  32. var _sunSpiderStartDate = new Date();
  33. 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");
  34. 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);
  35. colors = new Array("FF","CC","99","66","33","00");
  36. var endResult;
  37. function doTest()
  38. {
  39. endResult = "";
  40. // make up email address
  41. for (var k=0;k<4000;k++)
  42. {
  43. name = makeName(6);
  44. (k%2)?email=name+"@mac.com":email=name+"(at)mac.com";
  45. // validate the email address
  46. var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*)\.[a-zA-Z]{2,3}$/;
  47. if(pattern.test(email))
  48. {
  49. var r = email + " appears to be a valid email address.";
  50. addResult(r);
  51. }
  52. else
  53. {
  54. r = email + " does NOT appear to be a valid email address.";
  55. addResult(r);
  56. }
  57. }
  58. // make up ZIP codes
  59. for (var s=0;s<4000;s++)
  60. {
  61. var zipGood = true;
  62. var zip = makeNumber(4);
  63. (s%2)?zip=zip+"xyz":zip=zip.concat("7");
  64. // validate the zip code
  65. for (var i = 0; i < zip.length; i++) {
  66. var ch = zip.charAt(i);
  67. if (ch < "0" || ch > "9") {
  68. zipGood = false;
  69. r = zip + " contains letters.";
  70. addResult(r);
  71. }
  72. }
  73. if (zipGood && zip.length>5)
  74. {
  75. zipGood = false;
  76. r = zip + " is longer than five characters.";
  77. addResult(r);
  78. }
  79. if (zipGood)
  80. {
  81. r = zip + " appears to be a valid ZIP code.";
  82. addResult(r);
  83. }
  84. }
  85. }
  86. function makeName(n)
  87. {
  88. var tmp = "";
  89. for (var i=0;i<n;i++)
  90. {
  91. var l = Math.floor(26*Math.random());
  92. tmp += letters[l];
  93. }
  94. return tmp;
  95. }
  96. function makeNumber(n)
  97. {
  98. var tmp = "";
  99. for (var i=0;i<n;i++)
  100. {
  101. var l = Math.floor(9*Math.random());
  102. tmp = tmp.concat(l);
  103. }
  104. return tmp;
  105. }
  106. function addResult(r)
  107. {
  108. endResult += "\n" + r;
  109. }
  110. doTest();
  111. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  112. document.getElementById("console").innerHTML = _sunSpiderInterval;
  113. </script>
  114. </body>
  115. </html>