Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: cfform.js библиотечный файл :: qForms api-139, язык: javascript [code #152, hits: 10469]
автор: - [добавлен: 28.05.2006]
  1. qFormAPI.packages.cfform = true;
  2.  
  3. _addValidator(
  4. "isBoolean",
  5. function (){
  6. if(
  7. (this.value.toUpperCase() != "TRUE")
  8. || (this.value.toUpperCase() != "FALSE")
  9. || (this.value.toUpperCase() != "YES")
  10. || (this.value.toUpperCase() != "NO")
  11. || (this.value != "0")
  12. || (this.value != "1")
  13. ){
  14. this.error = "The " + this.description + " field does not contain a boolean value.";
  15. }
  16. }
  17. );
  18.  
  19. /**
  20. * A string GUID value is required. A GUID is a string
  21. * of length 36 formatted as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, where X is a
  22. * hexadecimal digit (0-9 or A-F).
  23. */
  24. _addValidator(
  25. "isGUID",
  26. function (){
  27. var v = _trim(this.value);
  28. if( !(/[A-Fa-f0-9]{8,8}-[A-Fa-f0-9]{4,4}-[A-Fa-f0-9]{4,4}-[A-Fa-f0-9]{4,4}-[A-Fa-f0-9]{12,12}/.test(v)) ){
  29. this.error = "The " + this.description + " field does not contain a valid GUID.";
  30. }
  31. }
  32. );
  33.  
  34. /**
  35. * A string UUID value is required. A UUID is a string
  36. * of length 35 formatted as XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX, where X is a
  37. * hexadecimal digit (0-9 or A-F).
  38. */
  39. _addValidator(
  40. "isUUID",
  41. function (){
  42. var v = _trim(this.value);
  43. if( !(/[A-Fa-f0-9]{8,8}-[A-Fa-f0-9]{4,4}-[A-Fa-f0-9]{4,4}-[A-Fa-f0-9]{16,16}/.test(v)) ){
  44. this.error = "The " + this.description + " field does not contain a valid GUID.";
  45. }
  46. }
  47. );
  48.  
  49.  
  50. /**
  51. * validate that the value is formatted correctly for a http/https/ftp url
  52. * This pattern will match http/https/ftp urls.
  53. *
  54. * Matches: http://www.mm.com/index.cfm
  55. * HTTP://WWW.MM.COM
  56. * http://www.mm.com/index.cfm?userid=1&name=mike+nimer
  57. * http://www.mm.com/index.cfm/userid/1/name/mike+nimer - trick used by cf developers so search engines can parse their sites (search engines ignore query strings)
  58. * ftp://www.mm.com/
  59. * ftp://uname:pass@www.mm.com/
  60. * mailto:email@address.com
  61. * news:rec.gardening
  62. * news:rec.gardening
  63. * http://a/
  64. * file://ftp.yoyodyne.com/pub/files/foobar.txt
  65. * Non-Matches: www.yahoo.com
  66. * http:www.mm.com
  67. *
  68. */
  69. _addValidator(
  70. "isURL",
  71. function (){
  72. var v = _trim(this.value).toLowerCase();
  73. if( !(/^((http|https|ftp|file)\:\/\/([a-zA-Z0-0]*:[a-zA-Z0-0]*(@))?[a-zA-Z0-9-\.]+(\.[a-zA-Z]{2,3})?(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9-\._\?\,\'\/\+&%\$#\=~])*)|((mailto)\:[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z0-9]{2,7})|((news)\:[a-zA-Z0-9\.]*)$/.test(v)) ){
  74. this.error = "The " + this.description + " field does not contain a valid URL.";
  75. }
  76. }
  77. );
  78.  
  79.  
  80. /**
  81. * A string UUID value is required. A UUID is a string
  82. * of length 35 formatted as XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX, where X is a
  83. * hexadecimal digit (0-9 or A-F).
  84. */
  85. _addValidator(
  86. "isRegEx",
  87. function (r){
  88. var v = _trim(this.value);
  89. if( !(r.test(v)) ){
  90. this.error = "The " + this.description + " field contains invalid data.";
  91. }
  92. }
  93. );
  94.  
  95. /**
  96. * validate that the value is formatted as a telephone correctly
  97. * This pattern matches any US Telephone Number.
  98. * This regular expression excludes the first number, after the area code,from being 0 or 1;
  99. * it also allows an extension to be added where it does not have to be prefixed by 'x'.
  100. *
  101. * Matches:
  102. * 617.219.2000
  103. * 219-2000
  104. * (617)283-3599 x234
  105. * 1(222)333-4444
  106. * 1 (222) 333-4444
  107. * 222-333-4444
  108. * 1-222-333-4444
  109. * Non-Matches:
  110. * 44-1344-458606
  111. * +44-1344-458606
  112. * +34-91-397-6611
  113. * 7-095-940-2000
  114. * +7-095-940-2000
  115. * +49-(0)-889-748-5516
  116. */
  117. _addValidator(
  118. "isUSPhone",
  119. function (){
  120. var v = _trim(this.value);
  121. if( !(/^(((1))?[ ,\-,\.]?([\\(]?([1-9][0-9]{2})[\\)]?))?[ ,\-,\.]?([^0-1]){1}([0-9]){2}[ ,\-,\.]?([0-9]){4}(( )((x){0,1}([0-9]){1,5}){0,1})?$/.test(v)) ){
  122. this.error = "The " + this.description + " field does not contain a valid phone number.";
  123. }
  124. }
  125. );
  126.  
  127.  
  128. _addValidator(
  129. "isTime",
  130. function (){
  131. var aTime = this.value.split(":");
  132. var isTime = true;
  133.  
  134. if( (this.value.length == 0) || (aTime.length != 2) ) isTime = false;
  135.  
  136. if( isTime ){
  137. var sHour = aTime[0];
  138. var iHour = parseInt(sHour, 10);
  139. var sMinute = aTime[1];
  140. var iMinute = parseInt(sMinute, 10);
  141.  
  142. if( (sHour != String(iHour)) || (sMinute != String(iMinute)) ) isTime = false;
  143. else if( (iHour < 0) || (iHour > 23) ) isTime = false;
  144. else if( (iMinute < 0) || (iMinute > 59) ) isTime = false;
  145. }
  146.  
  147. if( !isTime ){
  148. this.error = "The " + this.description + " field does not contain a valid time.";
  149. }
  150. }
  151. );
  152.  
  153. _addValidator(
  154. "isInt",
  155. function (){
  156. var v = this.value;
  157. var isNumeric = (v == String(parseFloat(v, 10)));
  158. if( !isNumeric || (parseInt(v, 10) != parseFloat(v, 10)) ){
  159. this.error = "The " + this.description + " field must contain an integer value.";
  160. }
  161. }
  162. );
  163.  
  164. _addValidator(
  165. "isFloat",
  166. function (){
  167. var v = this.value;
  168. var isNumeric = (v == String(parseFloat(v, 10)));
  169. if( !isNumeric || (parseInt(v, 10) == parseFloat(v, 10)) ){
  170. this.error = "The " + this.description + " field must contain an floating number.";
  171. }
  172. }
  173. );
  174.  
  175. _addValidator(
  176. "isNumber",
  177. function (){
  178. var v = this.value;
  179. if( v != String(parseFloat(v, 10)) ){
  180. this.error = "The " + this.description + " field must contain a valid number.";
  181. }
  182. }
  183. );
  184.  
  185.  
  186.  
/******************************************************************************
qForm JSAPI: CFFORM Validation Library

Author: Dan G. Switzer, II
Build: 100
******************************************************************************/
Тестировалось на: IE 6.0 SP2, Mozilla FF 1.5, Opera 8.5

+добавить реализацию