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;
} |