var BasicValidate = { ////////////////////////////////////////////////// Form: function(form, validate_event, required_class, error_class) { var form_id = (form.id != undefined) ? form.id : form; var field_errors = 0; var required_class = (required_class != undefined) ? required_class : 'mandatoryfield'; var error_class = (error_class != undefined) ? error_class : 'errorfield'; //---[ get fields ]--- var inputs = $(form_id).getInputs(); for( i=0; i < inputs.length; i++ ) { var field_name = inputs[i].name; if( $(field_name).hasClassName(required_class) ) { if( $(field_name).value.blank() ) { BasicValidate.SetError(field_name, true, error_class); field_errors++; } else if( field_name.indexOf("email") > -1 ) { BasicValidate.SetError(field_name, false); if( !BasicValidate.IsValidEmail( $(field_name).value ) ) { BasicValidate.SetError(field_name); field_errors++; } } else { BasicValidate.SetError(field_name, false, error_class); } } } if( validate_event != null ) field_errors += validate_event(this); if( field_errors == 0 ) return true; else return false; }, ////////////////////////////////////////////////// SetError: function(field_name, error_on, error_class ) { var error_class = (error_class != undefined ) ? error_class : 'errorfield'; if( error_on == undefined || error_on == true ) Element.addClassName(field_name, error_class); else Element.removeClassName(field_name, error_class); }, IsValidEmail: function(email) { var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; return pattern.test(email); }, SetCheckboxes: function(form_id) { if( $(form_id) ) { //---[ set method as post ]--- $(form_id).setAttribute('method', 'post'); //---[ set action /emailform ]--- //$(form_id).setAttribute('action', '/forms'); //---[ create checkbox index ]--- var inputs = $(form_id).getInputs('checkbox'); var cb_index = ''; for( i=0; i < inputs.length; i++ ) { cb_index += inputs[i].name; if( i < inputs.length -1 ) cb_index += ','; } //---[ add checkbox index to form ]--- if( !cb_index.blank() ) { var cbs = new Element('input', { 'type': 'hidden', 'name': 'checkbox_index', 'id': 'checkbox_index', 'value': cb_index }); $(form_id).insert(cbs); } } } };