// =========== global functions ========================= // Marks the form as invalid and adds an errormessage function error(message){ valid = false; error_text += "* " + message + "\r\n"; } // Gets a question name from a given ID function getName(id){ var label = document.getElementById(id + '_label'); var children = label.childNodes; for (var i = 0; i < children.length ; i++){ if(children[i].nodeType == 3){ return children[i].nodeValue; } } } function markAsBad(id){ var elem = document.getElementById(id); if(elem){ elem.oldClassName = elem.className; elem.className = 'badinput'; } var elem = document.getElementById(id+'_label'); if(elem){ elem.oldClassName = elem.className; elem.className = 'badinput'; } } function unmarkAll(){ unmark('input'); unmark('select'); unmark('textarea'); unmark('p') unmark('label'); } function unmark(tagname){ var elems = document.getElementsByTagName(tagname); for(var i = 0; i < elems.length; i++){ var elem = elems[i]; if(elem.className == 'badinput'){ elem.className = elem.oldClassName; elem.oldClassName = ''; } } } // ========= specific functions ============== // Validate against the bounds-option function validate_bounds(id, lowerBound, upperBound){ var value = parseInt(document.getElementById(id).value, 10); if(isNaN(value)){ return; } if(value < lowerBound || value > upperBound){ var name = getName(id); error("Answer to '" + name + "' out of bounds (must be between " + lowerBound + " and " + upperBound + ")"); markAsBad(id); } } // Validate against maxlength-option function validate_maxlength(id, maxlength){ var length = document.getElementById(id).value.length; if(length > maxlength){ var name = getName(id); error("Answer to '" + name + "' is too long (maximum " + maxlength + " characters)"); markAsBad(id); } } function validate_text(id){ // Empty } function validate_email(id){ var value = document.getElementById(id).value; if(value.length > 0){ var regex = /^[A-Za-z0-9_-]+(\.[A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z0-9]{2,4}$/; if(!regex.test(value)){ var name = getName(id); error("Answer to '" + name + "' is not a valid e-mail address"); markAsBad(id); } } } function validate_file(id){ // Empty } function validate_date(id){ var value = document.getElementById(id).value; if(value.length > 0){ if(!validDate(value)){ var name = getName(id); error("Answer to '" + name + "' is not a valid date (please format the date as dd-mm-yyyy)"); markAsBad(id); } } } function validate_number(id){ var value = document.getElementById(id).value; if(value.length > 0){ if(isNaN(parseInt(value))){ var name = getName(id); error("Answer to '" + name + "' is not a valid nubmer"); markAsBad(id); } } } function validate_choice(id){ // only possible validation is to check 'other' specification field var elem = document.getElementById(id); var otherOption = document.getElementById(id + '_optionother'); var name = getName(id); if(elem){ if(otherOption && otherOption.selected && document.getElementById(id + '_other').value == ''){ error("Please clarify your answer 'other' to '" + name + "'"); markAsBad(id); } }else{ if(otherOption && otherOption.checked && document.getElementById(id + '_other').value == ''){ error("Please clarify your answer 'other' to '" + name + "'"); markAsBad(id); } } } function validate_required(id, type){ var elem = document.getElementById(id); var name = getName(id); switch(type){ case 'choice': if(elem){ if(elem.selectedIndex == -1){ error("Question '" + name + "' was not answered"); markAsBad(id); } }else{ var optionName = 'q[' + id.substr(1) + '][options][]'; var optionElems = document.getElementsByName(optionName); found = false; for(var i = 0; i < optionElems.length; i++){ if(optionElems[i].checked){ found = true; } } if(!found){ error("Question '" + name + "' was not answered"); markAsBad(id); } } break; case 'email': case 'text': case 'file': case 'date': case 'number': case 'default': if(elem.value == ''){ error("Question '" + name + "' was not answered"); markAsBad(id); } break; } }