Login   Register  
PHP Classes
elePHPant
Icontem

File: validate.xsl

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Herman Veluwenkamp  >  HV WDDX Metadata  >  validate.xsl  >  Download  
File: validate.xsl
Role: Auxiliary data
Content type: text/plain
Description: Generates javascript array for client-side validation.
Class: HV WDDX Metadata
Generates XUL and HTML forms based on XML config.
Author: By
Last change: Description edited.
Date: 2003-12-07 13:11
Size: 4,246 bytes
 

Contents

Class file image Download
/**
* vaidate - Javascript Client-Side Validation
* @version 1.0alpha
*
* Copyright (C) 2003 Herman Veluwenkamp <hveluwenkamp@myrealbox.com>
* 
* This program is free software; you can redistribute it and/or modify it 
* under the terms of the GNU General Public License as published by the 
* Free Software Foundation; either version 2 of the License, or (at your 
* option) any later version.
* 
* This program is distributed in the hope that it will be useful, but 
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
* more details.
* 
* You should have received a copy of the GNU General Public License along 
* with this program; if not, write to the Free Software Foundation, Inc., 
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/

function validate(action) {
  var error_message = 'This form has the following validation errors.\n\n';
  var validation_error = false;
  var errorRow = null;
  var error_message_field = null;
  var encoded_request = '?_action=submit';
  var encoded_value = '';

  var name = null;
  var type = null;
  var description = null;
  var validate = null;
  var expected = null;
  var numOptions = null;
  var value = null;

  for (var name in field) {
    //name = i;
    type = field[name].type;
    description = field[name].description;
    validate = field[name].validate;
    expected = field[name].expected;
    numOptions = field[name].numOptions;
    value = 0;

    // skip over missing fields
    if (!document.getElementById(name) & !document.getElementById(name+'[1]')) continue;

    switch (type) {

      case 'popup' :
        element = document.getElementById(name);
        if (element.selectedIndex == -1) {
          if (element.value!='')  {
            encodedValue = escape(element.value);
            encoded_request +=  "&" + name + "=" + encodedValue;
            value = 1;
          }
        } else {
            encodedValue = escape(element.selectedItem.value)
            encoded_request +=  "&" + name + "=" + encodedValue;
            value=1;
        }
        break;

      case 'listbox' :
        for (i = 0; i < numOptions; i++) {
          element = document.getElementById(name+'['+(i+1)+']');

          if (element.selected) {
            encodedValue = escape(element.value)
            encoded_request +=  "&" + name + "%5B%5D=" + encodedValue;
            value++;
          }
        }
        break;

      case 'checklist' :
      case 'checkbox' :
        for (i = 0; i < numOptions; i++) {
          element = document.getElementById(name+'['+(i+1)+']');
          if (element.checked) {
            encodedValue = escape(element.getAttribute('value'));
            encoded_request +=  "&" + name + "%5B%5D=" + encodedValue;
            value++;
          }
        }
        break;

      case 'radio' :
        element = document.getElementById(name);
        if (element.selectedItem) {
            encodedValue = escape(element.selectedItem.value)
            encoded_request +=  "&" + name + "=" + encodedValue;
            value=1;
        }
        break;

      case 'password':
        element = document.getElementById(name+'[1]')
        value = element.value;
        encoded_value = hex_md5(escape(value));
        encoded_request +=  "&" + name + "%5B%5D=" + encoded_value;
        break;

      case 'date':
      case 'time':
      case 'text':
      case 'textbox':
        element = document.getElementById(name+'[1]')
        value = element.value;
        encoded_value = escape(value);
        encoded_request +=  "&" + name + "%5B%5D=" + encoded_value;
        break;

      default:
        alert('unknown field: ' + name + ' - ' + type);
        return false;
         break;
    }

    if (validate && !validate.test(value)) {
      validation_error = true;
      error_message += '"' + description + '"\n' + expected + '\n\n'
    }    
  }

  if (validation_error) {
    alert(error_message);
    return false;
  } else {
    document.NoLocation = action + encoded_request;
  }
}