Login   Register  
PHP Classes
elePHPant
Icontem

File: formClientValidated.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  >  xmlForm  >  formClientValidated.js  >  Download  
File: formClientValidated.js
Role: ???
Content type: text/plain
Description: Javascript for Client Validation Example
Class: xmlForm
Generates a form in HTML.
Author: By
Last change:
Date: 2002-02-26 00:16
Size: 1,454 bytes
 

Contents

Class file image Download
field = new Array();

function validate() {
  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];
    numOptions = field[i][5];
    value = 0;     
    
    form = document.inputForm;
    switch (type) {   
      case 'select' :
        for (i = 0; i < numOptions; i++)
          if (form.elements[name].options[i].selected) value++;
        break;
            
      case 'radio' :
        for (i = 0; i < numOptions; i++) 
          if (form.elements[name][i].checked) value++;
        break;
        
      case 'multiselect' :
        for (i = 0; i < numOptions; i++) 
          if (form.elements[name+'[]'].options[i].selected) value++;
        break;
       
      case 'checkbox' :
        for (i = 0; i < numOptions; i++)
          if (form.elements[name+'['+(i+1)+']'].checked) value++;
        break;
        
      default:
        value = form.elements[name].value;
    }
    
    //alert('name: ' + name + ' - regexp: ' + valid + ' - value: ' + value); 
    //alert('name: ' + name + ' - regexp: ' + valid + ' - value: ' + value + ' - test: ' + valid.test(value));
    
    if (!valid.test(value)) {
      error_message = 'Validation error in "' + description + '" field. \n' + expected;      
      alert(error_message);
      return false;
      break;
    }     
  }
  return true;
}