<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: functions.js библиотечный файл :: qForms api-139 [javascript, code #155, hits: 5434, рейтинг: 7/3,4.94(2187)] +
автор: - [добавлен: 28.05.2006] управление:
  1. function _trim(s){ return _rtrim(_ltrim(s)); }
  2.  
  3. function _ltrim(s){
  4. var w = " \n\t\f";
  5. // remove all beginning white space
  6. while( w.indexOf(s.charAt(0)) != -1 && s.length != 0 ) s = s.substring(1);
  7. return s;
  8. }
  9.  
  10. function _rtrim(s){
  11. var w = " \n\t\f";
  12. // remove all ending white space
  13. while( w.indexOf(s.charAt(s.length-1)) != -1 && s.length != 0 ) s = s.substring(0, s.length-1);
  14. return s;
  15. }
  16.  
  17. function _listToArray(string,delim){
  18. var delim = _param(arguments[1], ",");
  19. tmp = string.split(delim);
  20. for( var i=0; i < tmp.length; i++ ) tmp[i] = _trim(tmp[i]);
  21. return tmp;
  22. }
  23.  
  24. function _listSum(string,delim){
  25. var delim = _param(arguments[1], ",");
  26. tmp = _listToArray(string,delim);
  27. iValue = 0;
  28. for( var i=0; i < tmp.length; i++ ) iValue += parseInt(tmp[i], 10);
  29. return iValue;
  30. }
  31.  
  32. function _stripInvalidChars(string, type){
  33. var string = _param(arguments[0]);
  34. var type = _param(arguments[1], "numeric").toLowerCase();
  35. var validchars = type;
  36.  
  37. stcTypes = new Object();
  38. stcTypes.numeric = "1234567890";
  39. stcTypes.alpha = "abcdefghijklmnopqrstuvwxyz";
  40. stcTypes.alphnumeric = "abcdefghijklmnopqrstuvwxyz1234567890";
  41.  
  42. if( stcTypes[type] ) validchars = stcTypes[type];
  43.  
  44. var tmp = "";
  45. var lc = string.toLowerCase();
  46. // loop through the string an make sure each character is an alpha character
  47. for( var i=0;i < string.length;i++ ){
  48. if (validchars.indexOf(lc.charAt(i)) != -1) tmp += string.charAt(i);
  49. }
  50. return tmp;
  51. }
  52.  
  53. function _isLength(string, len, type){
  54. var string = _param(arguments[0]);
  55. var len = parseInt(_param(arguments[1], 10, "number"), 10);
  56. var type = _param(arguments[2], "numeric");
  57.  
  58. var tmp = _stripInvalidChars(string, type);
  59. return (tmp.length == len) ? true : false;
  60. }
  61.  
  62. function _getState(abbr){
  63. var abbr = _param(arguments[0]).toLowerCase();
  64. _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";
  65. if( !_s[abbr] ){
  66. return null;
  67. } else {
  68. return _s[abbr];
  69. }
  70. }
  71.  
  72. // define the default properties for the sort
  73. qFormAPI.sortOptions = new Object();
  74. qFormAPI.sortOptions.order = "asc";
  75. qFormAPI.sortOptions.byText = true;
  76. function _sortOptions(obj, order, byText){
  77. var order = _param(arguments[1], qFormAPI.sortOptions.order);
  78. if( order != "asc" && order != "desc" ) order = "asc";
  79. var byText = _param(arguments[2], qFormAPI.sortOptions.byText, "boolean");
  80. var orderAsc = (order == "asc") ? true : false;
  81.  
  82. // loop through all the options and sort them asc
  83. for( var i=0; i < obj.options.length; i++ ){
  84. for( var j=0; j < obj.options.length-1; j++ ){
  85. // if an option is greater than the next option, swap them
  86. if( orderAsc && (byText && obj.options[j].text > obj.options[j+1].text) || (!byText && obj.options[j].value > obj.options[j+1].value) ){
  87. tmpTxt = obj.options[j].text;
  88. tmpVal = obj.options[j].value;
  89. tmpSel = obj.options[j].selected;
  90. obj.options[j].text = obj.options[j+1].text ;
  91. obj.options[j].value = obj.options[j+1].value;
  92. obj.options[j].selected = obj.options[j+1].selected;
  93. obj.options[j+1].text = tmpTxt ;
  94. obj.options[j+1].value = tmpVal;
  95. obj.options[j+1].selected = tmpSel;
  96. } else if( !orderAsc && (byText && obj.options[j].text < obj.options[j+1].text) || (!byText && obj.options[j].value < obj.options[j+1].value) ){
  97. tmpTxt = obj.options[j].text;
  98. tmpVal = obj.options[j].value;
  99. tmpSel = obj.options[j].selected;
  100. obj.options[j].text = obj.options[j+1].text ;
  101. obj.options[j].value = obj.options[j+1].value;
  102. obj.options[j].selected = obj.options[j+1].selected;
  103. obj.options[j+1].text = tmpTxt ;
  104. obj.options[j+1].value = tmpVal;
  105. obj.options[j+1].selected = tmpSel;
  106. }
  107. }
  108. }
  109. return true;
  110. }
  111.  
  112. function _transferOptions(field1, field2, sort, type, selectItems, reset){
  113. var sort = _param(arguments[2], true, "boolean");
  114. var type = _param(arguments[3].toLowerCase(), "selected");
  115. if( type != "all" && type != "selected" ) type = "selected";
  116. var selectItems = _param(arguments[4], true, "boolean");
  117. var reset = _param(arguments[5], false, "boolean");
  118. var doAll = (type == "all") ? true : false;
  119.  
  120. 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.");
  121. 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.");
  122.  
  123. // clear the select box
  124. if( reset ) field2.length = 0;
  125.  
  126. for( var i=0; i < field1.length; i++ ){
  127. // if the current option is selected, move it
  128. if( doAll || field1.options[i].selected ){
  129. field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
  130. field1.options[i] = null;
  131. i--; // since you just deleted a option, redo this array position next loop
  132. }
  133. }
  134.  
  135. // if sorting the fields
  136. if( sort ) _sortOptions(field2);
  137. return true;
  138. }
  139.  
  140.  
  141. function _getURLParams(){
  142. struct = new Object();
  143. var strURL = document.location.href;
  144. var iPOS = strURL.indexOf("?");
  145. // if there are some query string params, split them into an array
  146. if( iPOS != -1 ){
  147. var strQS = strURL.substring(iPOS + 1);
  148. var aryQS = strQS.split("&");
  149.  
  150. // otherwise, return the empty structure
  151. } else {
  152. return struct;
  153. }
  154.  
  155. // loop through the array
  156. for( var i=0; i < aryQS.length; i++ ){
  157. iPOS = aryQS[i].indexOf("=");
  158. // if no equal sign is found, then the value is null
  159. if( iPOS == -1 ){
  160. struct[aryQS[i]] = null;
  161. // otherwise grab the variable name and it's value and stick it in structure
  162. } else {
  163. var key = aryQS[i].substring(0, iPOS);
  164. var value = unescape(aryQS[i].substring(iPOS+1));
  165. // if the value doesn't exist, then create a new key
  166. if( !struct[key] ) struct[key] = value;
  167. // otherwise, append the value
  168. else struct[key] += "," + value;
  169. }
  170. }
  171.  
  172. return struct;
  173. }
  174.  
  175. function _createFields(struct, type){
  176. var type = _param(arguments[1], "hidden");
  177. if( this.status == null ) return false;
  178. // loop through form elements
  179. for( key in struct ){
  180. document.write("<INPUT TYPE=\"" + type + "\" NAME=\"" + key + "\" VALUE=\"" + struct[key] + "\">");
  181. }
  182. return true;
  183. }
  184.  
  185. function _getEventType(type){
  186. // the default event type
  187. var strEvent = "onblur";
  188. // if this is a checkbox & radio button, then mirror value on click
  189. if( type == "checkbox" || type == "radio" ) strEvent = "onclick";
  190. // if this is a select box, then mirror value when the value changes
  191. else if( type.substring(0,6) == "select" ) strEvent = "onchange";
  192. return strEvent;
  193. }
  194.  
  195.  
/******************************************************************************
qForm JSAPI: Functions Library

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

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