<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: functions_js12.js библиотечный файл :: qForms api-139 [javascript, code #156, hits: 5897, рейтинг: 3/7,4.86(2092)] +
автор: - [добавлен: 28.05.2006] управление:
  1. function _trim(s){
  2. return _rtrim(_ltrim(s));
  3. }
  4.  
  5. function _ltrim(s){
  6. // remove all beginning white space
  7. return (s.length == 0) ? s : s.replace(new RegExp("^\\s+", qFormAPI.reAttribs), "");
  8. }
  9.  
  10. function _rtrim(s){
  11. // remove all ending white space
  12. return (s.length == 0) ? s : s.replace(new RegExp("\\s+$", qFormAPI.reAttribs), "");
  13. }
  14.  
  15. function _listToArray(string,delim){
  16. var delim = _param(arguments[1], ",");
  17. tmp = string.split(delim);
  18. for( var i=0; i < tmp.length; i++ ) tmp[i] = _trim(tmp[i]);
  19. return tmp;
  20. }
  21.  
  22. function _listSum(string,delim){
  23. var delim = _param(arguments[1], ",");
  24. tmp = _listToArray(string,delim);
  25. iValue = 0;
  26. for( var i=0; i < tmp.length; i++ ) iValue += parseInt(_trim(tmp[i]), 10);
  27. return iValue;
  28. }
  29.  
  30. function _stripInvalidChars(_s, _t){
  31. var s = _param(arguments[0]);
  32. var t = _param(arguments[1], "numeric").toLowerCase();
  33. var r;
  34.  
  35. if( t == "numeric" ) r = new RegExp("([^0-9.]*)(^-?\\d*\\.?\\d*)(\\D*)", qFormAPI.reAttribs);
  36. else if( t == "alpha" ) r = new RegExp("[^A-Za-z]+", qFormAPI.reAttribs);
  37. else if( t == "alphanumeric" ) r = new RegExp("\\W+", qFormAPI.reAttribs);
  38. else r = new RegExp("[^" + t + "]+", qFormAPI.reAttribs);
  39.  
  40. if( t == "numeric" ) s = s.replace(r, "$2");
  41. else s = s.replace(r, "");
  42.  
  43. return s;
  44. }
  45.  
  46. function _isLength(string, len, type){
  47. var string = _param(arguments[0]);
  48. var len = parseInt(_param(arguments[1], 10, "number"), 10);
  49. var type = _param(arguments[2], "numeric");
  50.  
  51. var tmp = _stripInvalidChars(string, type);
  52. return (tmp.length == len) ? true : false;
  53. }
  54.  
  55. function _getState(abbr){
  56. var abbr = _param(arguments[0]).toLowerCase();
  57. _s = new Object(); _s.al = "Alabama"; _s.ak = "Alaska"; _s.as = "American Samoa"; _s.az = "Arizona"; _s.ar = "Arkansas"; _s.ca = "California"; _s.co = "Colorado"; _s.ct = "Connecticut"; _s.de = "Delaware"; _s.dc = "District of Columbia"; _s.fm = "Federal States of Micronesia"; _s.fl = "Florida"; _s.ga = "Georgia"; _s.gu = "Guam"; _s.hi = "Hawaii"; _s.id = "Idaho"; _s.il = "Illinois"; _s["in"] = "Indiana"; _s.ia = "Iowa"; _s.ks = "Kansas"; _s.ky = "Kentucky"; _s.la = "Louisana"; _s.me = "Maine"; _s.mh = "Marshall Islands"; _s.md = "Maryland"; _s.ma = "Massachusetts"; _s.mi = "Michigan"; _s.mn = "Minnesota"; _s.ms = "Mississippi"; _s.mo = "Missouri"; _s.mt = "Montana"; _s.ne = "Nebraska"; _s.nv = "Nevada"; _s.nh = "New Hampshire"; _s.nj = "New Jersey"; _s.nm = "New Mexico"; _s.ny = "New York"; _s.nc = "North Carolina"; _s.nd = "North Dakota"; _s.mp = "Northern Mariana Islands"; _s.oh = "Ohio"; _s.ok = "Oklahoma"; _s.or = "Oregon"; _s.pw = "Palau"; _s.pa = "Pennsylvania"; _s.pr = "Puerto Rico"; _s.ri = "Rhode Island"; _s.sc = "South Carolina"; _s.sd = "South Dakota"; _s.tn = "Tennessee"; _s.tx = "Texas"; _s.ut = "Utah"; _s.vt = "Vermont"; _s.vi = "Virgin Islands"; _s.va = "Virginia"; _s.wa = "Washington"; _s.wv = "West Virginia"; _s.wi = "Wisconsin"; _s.wy = "Wyoming"; _s.aa = "Armed Forces Americas"; _s.ae = "Armed Forces Africa/Europe/Middle East"; _s.ap = "Armed Forces Pacific";
  58. if( !_s[abbr] ){
  59. return null;
  60. } else {
  61. return _s[abbr];
  62. }
  63. }
  64.  
  65. // define the default properties for the sort
  66. qFormAPI.sortOptions = new Object();
  67. qFormAPI.sortOptions.order = "asc";
  68. qFormAPI.sortOptions.byText = true;
  69.  
  70. function _sortOptions(obj, order, byText){
  71. var order = _param(arguments[1], qFormAPI.sortOptions.order);
  72. if( order != "asc" && order != "desc" ) order = "asc";
  73. var byText = _param(arguments[2], qFormAPI.sortOptions.byText, "boolean");
  74. var orderAsc = (order == "asc") ? true : false;
  75.  
  76. // loop through all the options and sort them asc
  77. for( var i=0; i < obj.options.length; i++ ){
  78. for( var j=0; j < obj.options.length-1; j++ ){
  79. // if an option is greater than the next option, swap them
  80. if( orderAsc && (byText && obj.options[j].text > obj.options[j+1].text) || (!byText && obj.options[j].value > obj.options[j+1].value) ){
  81. _swapOptions(obj.options[j], obj.options[j+1]);
  82. } else if( !orderAsc && (byText && obj.options[j].text < obj.options[j+1].text) || (!byText && obj.options[j].value < obj.options[j+1].value) ){
  83. _swapOptions(obj.options[j], obj.options[j+1]);
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89.  
  90. function _swapOptions(o1, o2){
  91. var sText = o1.text;
  92. var sValue = o1.value;
  93. var sSelected = o1.selected;
  94. o1.text = o2.text ;
  95. o1.value = o2.value;
  96. o1.selected = o2.selected;
  97. o2.text = sText;
  98. o2.value = sValue;
  99. o2.selected = sSelected;
  100. }
  101.  
  102. function _transferOptions(field1, field2, sort, type, selectItems, reset){
  103. var sort = _param(arguments[2], true, "boolean");
  104. var type = _param(arguments[3], "selected").toLowerCase();
  105. if( type != "all" && type != "selected" ) type = "selected";
  106. var selectItems = _param(arguments[4], true, "boolean");
  107. var reset = _param(arguments[5], false, "boolean");
  108. var doAll = (type == "all") ? true : false;
  109.  
  110. if( field1.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
  111. if( field2.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");
  112.  
  113. // clear the select box
  114. if( reset ) field2.length = 0;
  115.  
  116. for( var i=0; i < field1.length; i++ ){
  117. // if the current option is selected, move it
  118. if( doAll || field1.options[i].selected ){
  119. field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
  120. field1.options[i] = null;
  121. i--; // since you just deleted a option, redo this array position next loop
  122. }
  123. }
  124.  
  125. // if sorting the fields
  126. if( sort ) _sortOptions(field2);
  127. return true;
  128. }
  129.  
  130.  
  131. function _getURLParams(){
  132. struct = new Object();
  133. var strURL = document.location.href;
  134. var iPOS = strURL.indexOf("?");
  135. // if there are some query string params, split them into an array
  136. if( iPOS != -1 ){
  137. var strQS = strURL.substring(iPOS + 1);
  138. var aryQS = strQS.split("&");
  139. // otherwise, return the empty structure
  140. } else {
  141. return struct;
  142. }
  143.  
  144. // loop through the array
  145. for( var i=0; i < aryQS.length; i++ ){
  146. iPOS = aryQS[i].indexOf("=");
  147. // if no equal sign is found, then the value is null
  148. if( iPOS == -1 ){
  149. struct[aryQS[i]] = null;
  150. // otherwise grab the variable name and it's value and stick it in structure
  151. } else {
  152. var key = aryQS[i].substring(0, iPOS);
  153. var value = unescape(aryQS[i].substring(iPOS+1));
  154. // if the value doesn't exist, then create a new key
  155. if( !struct[key] ) struct[key] = value;
  156. // otherwise, append the value
  157. else struct[key] += "," + value;
  158. }
  159. }
  160. return struct;
  161. }
  162.  
  163. function _createFields(struct, type){
  164. var type = _param(arguments[1], "hidden");
  165. if( this.status == null ) return false;
  166. // loop through form elements
  167. for( key in struct ){
  168. document.write("<input type=\"" + type + "\" name=\"" + key + "\" value=\"" + struct[key] + "\" />");
  169. }
  170. return true;
  171. }
  172.  
  173. function _getEventType(type){
  174. // the default event type
  175. var strEvent = "onblur";
  176. // if this is a checkbox & radio button, then mirror value on click
  177. if( type == "checkbox" || type == "radio" ) strEvent = "onclick";
  178. // if this is a select box, then mirror value when the value changes
  179. else if( type.substring(0,6) == "select" ) strEvent = "onchange";
  180. return strEvent;
  181. }
/******************************************************************************
qForm JSAPI: Functions Library

Author: Dan G. Switzer, II
Build: 110
******************************************************************************/

+добавить реализацию
 
каталог | задачи | паттерны | исходники | стат | форумы | карта сайта | контакты | ссылки 
© 2000-2018 CodeLAB Group
  Все права защищены
Страница сгенерирована за 0.002218 секунд
Количество запросов к БД: 7, gzip: 8.1kb/52.6kb(85%)