Login   Register  
PHP Classes
elePHPant
Icontem

File: validate.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Herman Veluwenkamp  >  form_template  >  validate.js  >  Download  
File: validate.js
Role: ???
Content type: text/plain
Description: Javascript for client-side validation.
Class: form_template
Form generation and validation class.
Author: By
Last change:
Date: 2000-05-30 19:45
Size: 1,630 bytes
 

Contents

Class file image Download
field = new Array();

function validate(action) {
  document.input_form.form_action.value = action;
  if (action=='cancel') { document.input_form.submit(); return; }
  
  form_ok = true;  

  for (var i in field) {   
    name = field[i][0];
    type = field[i][1];
    description = field[i][2];
    valid = field[i][3];
    expected = field[i][4];
    num_options = field[i][5];
    value = 0;
    //extra = '';
    
    switch (type) {   
      case 'select' :
        for (i = 0; i < num_options; i++)
          if (document.input_form.elements[name].options[i].selected) value++;
        break;
            
      case 'radio' :
        for (i = 0; i < num_options; i++) 
          if (document.input_form.elements[name][i].checked) value++;
        break;
        
      case 'multi' :
        for (i = 0; i < num_options; i++) 
          if (document.input_form.elements[name+'[]'].options[i].selected) value++;
        break;
        
      case 'check' :
        for (i = 0; i < num_options; i++)
          if (document.input_form.elements[name+'['+i+']'].checked) value++;
        break;
        
      default:
        value = document.input_form.elements[name].value;
    }
    
    if (expected) extra = expected;
    //alert("name: " + name + ' - regexp: '+valid+' - value: ' + value + ' - test: '+valid.test(value));
    if (!valid.test(value)) {
      error_message = 'Validation error in "'+description+'" field. \n'+extra;      
      alert(error_message);
      form_ok = false;
      break;
    }
        
  }
  if (form_ok) document.input_form.submit();
}