/** * Check if a field is valid * @param Object element to check * @param Integer type of the element (0 is text, 1 is email, 2 is number) * @param String Style of the error displayed (default = inline) * @param Boolean True if the field can be empty, False otherwise * @return True if OK, False otherwise */ function piwecard_checkField(element, elementType, display, nullIsOK) { var TEXT = 0; var EMAIL = 1; var NUMBER = 2; nullIsOK = (typeof nullIsOK === "undefined") ? false : nullIsOK; display = (typeof display === "undefined") ? 'inline' : display; if (nullIsOK && element.value === '') { return true; } else { switch (elementType) { case TEXT: if (element.value == '' || element.className.indexOf("ecard_defaultTextActive") > 0) { add_error_style(element, display); return false; } else { remove_error_style(element); return true; } break; case EMAIL: var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (!filter.test(element.value)) { add_error_style(element, display); return false; } else { remove_error_style(element); return true; } break; case NUMBER: if (element.value == '') { add_error_style(element, display); return false; } else { if (parseFloat(element.value) % 1 == 0){ remove_error_style(element); return true; } else { add_error_style(element, display); return false; } } break; default: return false; break; } } } /** * Style to add to the field if piwecard_checkField returns false * @param Object element * @param String Style of the error displayed (default = inline) */ function add_error_style(element, display) { document.getElementById(element.id+'_error').style.display = display; element.className += " ecard_error_input"; } /** * Remove the error style from the field if piwecard_checkField returns true * @param Object element */ function remove_error_style(element) { document.getElementById(element.id+'_error').style.display = 'none'; element.className = element.className.replace( /(?:^|\s)ecard_error_input(?!\S)/g , ''); }