Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: validation_addon.js :: qForms api-139, язык: javascript [code #158, hits: 8795]
автор: - [добавлен: 28.05.2006]
  1. qFormAPI.packages.validation = true;
  2.  
  3. // [start] validation routine
  4. function _f_isAtLeastOne(_f){
  5. var sFields = this.name + ((typeof _f == "string") ? "," + _removeSpaces(_f) : "");
  6. var aFields = sFields.split(","), v = new Array(), d = new Array(), x = ",";
  7.  
  8. for( var i=0; i < aFields.length; i++ ){
  9. if( !this.qForm[aFields[i]] ) return alert("The field name \"" + aFields[i] + "\" does not exist.");
  10. // store the value in an array
  11. v[v.length] = this.qForm[aFields[i]].getValue();
  12. // if the field name is already in the list, don't add it
  13. if( x.indexOf("," + aFields[i] + ",") == -1 ){
  14. d[d.length] = this.qForm[aFields[i]].description;
  15. x += aFields[i] + ",";
  16. }
  17. }
  18.  
  19. // if all of the form fields has empty lengths, then throw
  20. // an error message to the page
  21. if( v.join("").length == 0 ){
  22. this.error = "At least one of the following fields is required:\n " + d.join(", ");
  23. for( i=0; i < aFields.length; i++ ){
  24. if( qFormAPI.useErrorColorCoding && this.qForm[aFields[i]].obj.style ) this.qForm[aFields[i]].obj.style[qFormAPI.styleAttribute] = qFormAPI.errorColor;
  25. }
  26. }
  27. }
  28. _addValidator("isAtLeastOne", _f_isAtLeastOne, true);
/******************************************************************************
qForm JSAPI: Add-on Validation Library

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

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