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