Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: validation.js :: qForms api-139, язык: javascript [code #157, hits: 9913]
автор: - [добавлен: 28.05.2006]
  1. qFormAPI.packages.validation = true;
  2.  
  3. // define Field isNotNull(); prototype
  4. function _Field_isNotNull(){
  5. // check for blank field
  6. if( this.value.length == 0 ){
  7. this.error = "You must specify a(n) " + this.description + ".";
  8. }
  9. }
  10. _addValidator("isNotNull", _Field_isNotNull, true);
  11.  
  12. // define Field isNotEmpty(); prototype
  13. function _Field_isNotEmpty(){
  14. // check for blank field
  15. if( _ltrim(this.value).length == 0 ){
  16. this.error = "You must specify a(n) " + this.description + ".";
  17. }
  18. }
  19. _addValidator("isNotEmpty", _Field_isNotEmpty);
  20.  
  21. // define Field isEmail(); prototype
  22. function _Field_isEmail(){
  23. // check for @ . or blank field
  24. if( this.value.indexOf(" ") != -1 ){
  25. this.error = "Invalid " + this.description + " address. An e-mail address should not contain a space.";
  26. } else if( this.value.indexOf("@") == -1 ){
  27. this.error = "Invalid " + this.description + " address. An e-mail address must contain the @ symbol.";
  28. } else if( this.value.indexOf("@") == 0 ){
  29. this.error = "Invalid " + this.description + " address. The @ symbol can not be the first character of an e-mail address.";
  30. } else if( this.value.substring(this.value.indexOf("@")+2).indexOf(".") == -1 ){
  31. this.error = "Invalid " + this.description + " address. An e-mail address must contain at least one period after the @ symbol.";
  32. } else if( this.value.lastIndexOf("@") == this.value.length-1 ){
  33. this.error = "Invalid " + this.description + " address. The @ symbol can not be the last character of an e-mail address.";
  34. } else if( this.value.lastIndexOf(".") == this.value.length-1 ){
  35. this.error = "Invalid " + this.description + " address. A period can not be the last character of an e-mail address.";
  36. }
  37. }
  38. _addValidator("isEmail", _Field_isEmail);
  39.  
  40. // define Field isPassword(); prototype
  41. function _Field_isPassword(field, minlen, maxlen){
  42. var minlen = _param(arguments[1], 1, "number"); // default minimum length of password
  43. var maxlen = _param(arguments[2], 255, "number"); // default maximum length of password
  44.  
  45. if( field != null && !this.compare(field) ){
  46. this.error = "The " + this.description + " and " + this.qForm[field].description + " values do not match.";
  47. }
  48.  
  49. // if there aren't an errors yet
  50. if( this.error.length == 0 ){
  51. if( (this.value.length < minlen) || (this.value.length > maxlen) ){
  52. this.error = "The " + this.description + " field must be between " + minlen.toString() + " and " + maxlen.toString() + " characters long.";
  53. }
  54. }
  55. }
  56. _addValidator("isPassword", _Field_isPassword);
  57.  
  58. // define Field isSame(); prototype
  59. function _Field_isSame(field){
  60. if( !this.compare(field) ){
  61. this.error = "The " + this.description + " and " + this.qForm[field].description + " values do not match.";
  62. }
  63. }
  64. _addValidator("isSame", _Field_isSame);
  65.  
  66. // define Field isDifferent(); prototype
  67. function _Field_isDifferent(field){
  68. if( this.compare(field) ){
  69. this.error = "The " + this.description + " and " + this.qForm[field].description + " must be different.";
  70. }
  71. }
  72. _addValidator("isDifferent", _Field_isDifferent);
  73.  
  74. // define Field isRange(); prototype
  75. function _Field_isRange(low, high){
  76. var low = _param(arguments[0], 0, "number");
  77. var high = _param(arguments[1], 9999999, "number");
  78. var iValue = parseInt(this.value, 10);
  79. if( isNaN(iValue) ) iValue = 0;
  80.  
  81. // check to make sure the number is within the valid range
  82. if( ( low > iValue ) || ( high < iValue ) ){
  83. this.error = "The " + this.description + " field does not contain a\nvalue between " + low + " and " + high + ".";
  84. }
  85. }
  86. _addValidator("isRange", _Field_isRange);
  87.  
  88. // define Field isInteger(); prototype
  89. function _Field_isInteger(){
  90. var i = parseInt(this.value, 10);
  91. // make sure the user specified a numeric value
  92. if( isNaN(i) || (String(i) != this.value) ){
  93. this.error = "The value for " + this.description + " is not a numeric value. This field requires a numeric value.";
  94. }
  95. }
  96. _addValidator("isInteger", _Field_isInteger);
  97.  
  98. // define Field isNumeric(); prototype
  99. function _Field_isNumeric(){
  100. var i = parseFloat(this.value, 10);
  101. // make sure the user specified a numeric value
  102. if( isNaN(i) || (String(i) != this.value) ){
  103. this.error = "The value for " + this.description + " is not a numeric value. This field requires a numeric value.";
  104. }
  105. }
  106. _addValidator("isNumeric", _Field_isNumeric);
  107.  
  108. // define Field isAlpha(); prototype
  109. function _Field_isAlpha(){
  110. if( !_isLength(this.value, this.value.length, "alpha") ){
  111. this.error = "The value for " + this.description + " must contain only alpha characters.";
  112. }
  113. }
  114. _addValidator("isAlpha", _Field_isAlpha);
  115.  
  116. // define Field isAlphaNumeric(); prototype
  117. function _Field_isAlphaNumeric(){
  118. if( !_isLength(this.value, this.value.length, "alphanumeric") ){
  119. this.error = "The value for " + this.description + " must contain only alpha-numeric characters.";
  120. }
  121. }
  122. _addValidator("isAlphaNumeric", _Field_isAlphaNumeric);
  123.  
  124. // define Field isDate(); prototype
  125. function _Field_isDate(mask){
  126. var strMask = _param(arguments[0], "mm/dd/yyyy");
  127. var iMaskMonth = strMask.lastIndexOf("m") - strMask.indexOf("m") + 1;
  128. var iMaskDay = strMask.lastIndexOf("d") - strMask.indexOf("d") + 1;
  129. var iMaskYear = strMask.lastIndexOf("y") - strMask.indexOf("y") + 1;
  130.  
  131. var strDate = this.value;
  132.  
  133. // find the delimiter
  134. var delim = "", lstMask = "mdy";
  135. for( var i=0; i < strMask.length; i++ ){
  136. if (lstMask.indexOf(strMask.substring(i, i+1)) == -1){
  137. delim = strMask.substring(i, i+1);
  138. break;
  139. }
  140. }
  141. aMask = strMask.split(delim);
  142. if( aMask.length == 3 ){
  143. dt = this.value.split(delim);
  144. if( dt.length != 3 ) this.error = "An invalid date was provided for " + this.description + " field.";
  145. for( i=0; i < aMask.length; i++ ){
  146. if( aMask[i].indexOf("m") > -1 ) var sMonth = dt[i];
  147. else if( aMask[i].indexOf("d") > -1 ) var sDay = dt[i];
  148. else if( aMask[i].indexOf("y") > -1 ) var sYear = dt[i];
  149. }
  150. } else if( mask.length == 1 ){
  151. var sMonth = this.value.substring(strMask.indexOf("m")-1, strMask.lastIndexOf("m"));
  152. var sDay = this.value.substring(strMask.indexOf("d")-1, strMask.lastIndexOf("d"));
  153. var sYear = this.value.substring(strMask.indexOf("y")-1, strMask.lastIndexOf("y"));
  154. } else {
  155. this.error = "An invalid date mask was provided for " + this.description + " field.";
  156. }
  157.  
  158. var iMonth = parseInt(sMonth, 10);
  159. var iDay = parseInt(sDay, 10);
  160. var iYear = parseInt(sYear, 10);
  161.  
  162. if( isNaN(iMonth) || sMonth.length > iMaskMonth ) iMonth = 0;
  163. if( isNaN(iDay) || sDay.length > iMaskDay ) iDay = 0;
  164. if( isNaN(sYear) || sYear.length != iMaskYear ) sYear = null;
  165.  
  166. lst30dayMonths = ",4,6,9,11,";
  167.  
  168. if( sYear == null ){
  169. this.error = "An invalid year was provided for the " + this.description + " field. The year \n should be a " + iMaskYear + " digit number.";
  170. } else if( (iMonth < 1) || (iMonth > 12 ) ){
  171. this.error = "An invalid month was provided for " + this.description + " field.";
  172. } else {
  173. if( iYear < 100 ) var iYear = iYear + ((iYear > 20) ? 1900 : 2000);
  174. var iYear = (sYear.length == 4) ? parseInt(sYear, 10) : parseInt("20" + sYear, 10);
  175. if( lst30dayMonths.indexOf("," + iMonth + ",") > -1 ){
  176. if( (iDay < 1) || (iDay > 30 ) ) this.error = "An invalid day was provided for the " + this.description + " field.";
  177. } else if( iMonth == 2 ){
  178. if( (iDay < 1) || (iDay > 28 && !( (iDay == 29) && (iYear%4 == 0 ) ) ) ) this.error = "An invalid day was provided for the " + this.description + " field.";
  179. } else {
  180. if( (iDay < 1) || (iDay > 31 ) ) this.error = "An invalid day was provided for the " + this.description + " field.";
  181. }
  182. }
  183.  
  184. }
  185. _addValidator("isDate", _Field_isDate);
  186.  
  187.  
  188. // define Field isCreditCard(); prototype
  189. function _Field_isCreditCard(){
  190. var strCC = _stripInvalidChars(this.value, "numeric").toString();
  191. var isNumeric = (strCC.length > 0) ? true : false;
  192.  
  193. if( isNumeric ){
  194. // now check mod10
  195. var dd = (strCC.length % 2 == 1) ? false : true;
  196. var cd = 0;
  197. var td;
  198.  
  199. for( var i=0; i < strCC.length; i++ ){
  200. td = parseInt(strCC.charAt(i), 10);
  201. if( dd ){
  202. td *= 2;
  203. cd += (td % 10);
  204. if ((td / 10) >= 1.0) cd++;
  205. dd = false;
  206. } else {
  207. cd += td;
  208. dd = true;
  209. }
  210. }
  211. if( (cd % 10) != 0 ) this.error = "The credit card number entered in the " + this.description + " field is invalid.";
  212. } else {
  213. this.error = "The credit card number entered in the " + this.description + " field is invalid.";
  214. }
  215. }
  216. _addValidator("isCreditCard", _Field_isCreditCard);
  217.  
  218. // define Field isPhoneNumber(); prototype
  219. function _Field_isPhoneNumber(len){
  220. var len = parseInt(_param(arguments[0], 10, "number"), 10);
  221. var description = (this.description == this.name.toLowerCase()) ? "phone number" : this.description;
  222.  
  223. // check to make sure the phone is the correct length
  224. if( !_isLength(this.value, len) ){
  225. this.error = "The " + description + " field must include " + len + " digits.";
  226. }
  227. }
  228. _addValidator("isPhoneNumber", _Field_isPhoneNumber);
  229.  
  230. // define Field isLength(); prototype
  231. function _Field_isLength(len, type){
  232. var len = parseInt(_param(arguments[0], 10, "number"), 10);
  233. var type = _param(arguments[1], "numeric");
  234.  
  235. // check to make sure the phone is the correct length
  236. if( !_isLength(this.value, len, type) ){
  237. this.error = "The " + this.description + " field must include " + len + " " + type + " characters.";
  238. }
  239. }
  240. _addValidator("isLength", _Field_isLength);
  241.  
  242. // define Field isSSN(); prototype
  243. function _Field_isSSN(){
  244. var description = (this.description == this.name.toLowerCase()) ? "social security" : this.description;
  245.  
  246. // check to make sure the phone is the correct length
  247. if( !_isLength(this.value, 9) ){
  248. this.error = "The " + description + " field must include 9 digits.";
  249. }
  250. }
  251. _addValidator("isSSN", _Field_isSSN);
  252.  
  253.  
  254. // define Field isState(); prototype
  255. function _Field_isState(){
  256. // check to make sure the phone is the correct length
  257. if( _getState(this.value) == null ){
  258. this.error = "The " + this.description + " field must contain a valid 2-digit state abbreviation.";
  259. }
  260. }
  261. _addValidator("isState", _Field_isState);
  262.  
  263. // define Field isZipCode(); prototype
  264. function _Field_isZipCode(){
  265. var description = (this.description == this.name.toLowerCase()) ? "zip code" : this.description;
  266.  
  267. iZipLen = _stripInvalidChars(this.value).length;
  268.  
  269. // check to make sure the zip code is the correct length
  270. if( iZipLen != 5 && iZipLen != 9 ){
  271. this.error = "The " + description + " field must contain either 5 or 9 digits.";
  272. }
  273. }
  274. _addValidator("isZipCode", _Field_isZipCode);
  275.  
  276. // define Field isFormat(); prototype
  277. function _Field_isFormat(mask, type){
  278. var mask = _param(arguments[0]);
  279. var type = _param(arguments[1], "numeric").toLowerCase();
  280. var strErrorMsg = "";
  281.  
  282. var strMaskLC = mask.toLowerCase();
  283. // define quick masks
  284. if( strMaskLC == "ssn" ){
  285. mask = "xxx-xx-xxxx";
  286. type = "numeric";
  287. var description = (this.description == this.name.toLowerCase()) ? "social security number" : this.description;
  288. strErrorMsg = "The " + description + " field must contain 9 digits and \nshould be in the format: " + mask;
  289.  
  290. } else if( (strMaskLC == "phone") || (strMaskLC == "phone1") ){
  291. mask = "(xxx) xxx-xxxx";
  292. type = "numeric";
  293. var description = (this.description == this.name.toLowerCase()) ? "phone number" : this.description;
  294. strErrorMsg = "The " + description + " field must contain 10 digits and \nshould be in the format: " + mask;
  295.  
  296. } else if( strMaskLC == "phone2" ){
  297. mask = "xxx-xxx-xxxx";
  298. type = "numeric";
  299. var description = (this.description == this.name.toLowerCase()) ? "phone number" : this.description;
  300. strErrorMsg = "The " + description + " field must contain 10 digits and \nshould be in the format: " + mask;
  301.  
  302. } else if( strMaskLC == "phone3" ){
  303. mask = "xxx/xxx-xxxx";
  304. type = "numeric";
  305. var description = (this.description == this.name.toLowerCase()) ? "phone number" : this.description;
  306. strErrorMsg = "The " + description + " field must contain 10 digits and \nshould be in the format: " + mask;
  307.  
  308. } else if( strMaskLC == "phone7" ){
  309. mask = "xxx-xxxx";
  310. type = "numeric";
  311. var description = (this.description == this.name.toLowerCase()) ? "phone number" : this.description;
  312. strErrorMsg = "The " + description + " field must contain 7 digits and \nshould be in the format: " + mask;
  313.  
  314. } else if( strMaskLC == "zip" ){
  315. if( _stripInvalidChars(this.value).length < 6 ){
  316. mask = "xxxxx";
  317. } else {
  318. mask = "xxxxx-xxxx";
  319. }
  320. type = "numeric";
  321. var description = (this.description == this.name.toLowerCase()) ? "zip code" : this.description;
  322. strErrorMsg = "The " + description + " field should contain either 5 or 9 digits \nand be in the format: xxxxx or xxxxx-xxxx";
  323.  
  324. } else if( strMaskLC == "zip5" ){
  325. mask = "xxxxx";
  326. type = "numeric";
  327. var description = (this.description == this.name.toLowerCase()) ? "zip code" : this.description;
  328. strErrorMsg = "The " + description + " field should contain 5 digits \nand be in the format: " + mask;
  329.  
  330. } else if( strMaskLC == "zip9" ){
  331. mask = "xxxxx-xxxx";
  332. type = "numeric";
  333. var description = (this.description == this.name.toLowerCase()) ? "zip code" : this.description;
  334. strErrorMsg = "The " + description + " field should contain 9 digits \nand be in the format: " + mask;
  335. } else {
  336. var description = this.description;
  337. }
  338.  
  339. var string = _stripInvalidChars(this.value, type);
  340. var masklen = _stripInvalidChars(mask, "x").length;
  341.  
  342. // check to make sure the string contains the correct number of characters
  343. if( string.length != masklen && this.value.length > 0){
  344. if( strErrorMsg.length == 0 ) strErrorMsg = "This field requires at least " + masklen + " valid characters. Please \nmake sure to enter the value in the format: \n " + mask + "\n(where 'x' is a valid character.)";
  345. this.error = strErrorMsg;
  346.  
  347. // else re-format the string as defined by the mask
  348. } else if( string.length == masklen ){
  349. // find the position of all non "X" characters
  350. var stcMask = new Object();
  351. var lc = mask.toLowerCase();
  352. // loop through the string an make sure each character is an valid character
  353. for( var i=0; i < mask.length; i++ ){
  354. if( lc.charAt(i) != "x" ) stcMask[i] = mask.charAt(i);
  355. }
  356.  
  357. // put all the non-"X" characters back into the parsed string
  358. var iLastChar = 0;
  359. var newString = "";
  360. var i = 0;
  361. for( var pos in stcMask ){
  362. pos = parseInt(pos, 10);
  363. if( pos > iLastChar ){
  364. newString += string.substring(iLastChar, pos-i) + stcMask[pos];
  365. iLastChar = pos-i;
  366. } else {
  367. newString += stcMask[pos];
  368. }
  369. i++;
  370. }
  371. if( i == 0 ){
  372. newString = string;
  373. } else {
  374. newString += string.substring(iLastChar);
  375. }
  376.  
  377. // set the value of the field to the new string--make sure not to perform the onBlur event
  378. this.setValue(newString, true, false);
  379. }
  380. }
  381. _addValidator("isFormat", _Field_isFormat);
  382.  
  383. // define Field isLengthGT(); prototype
  384. function _Field_isLengthGT(len){
  385. if( this.obj.value.length <= len){
  386. this.error = "The " + this.description + " field must be greater than " + len + " characters.";
  387. }
  388. }
  389. _addValidator("isLengthGT", _Field_isLengthGT);
  390.  
  391. // define Field isLengthLT(); prototype
  392. function _Field_isLengthLT(len){
  393. if( this.obj.value.length >= len){
  394. this.error = "The " + this.description + " field must be less than " + len + " characters.";
  395. }
  396. }
  397. _addValidator("isLengthLT", _Field_isLengthLT);
/******************************************************************************
qForm JSAPI: Validation Library

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

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