/* USAGE example class name: jj_y_n_0_3m45 (Initials_Required_Type_Format[_Length]) Length is optional Initials - needed to determine if the class name is used for validation can be any string of alpha-numeric characters as defined by the variable "initials" Required - needed to determine if the value is required for submission y = required n = not required, but if there is data in the field then validate it Type - used to determine the validation type to be used on the elements data n = number s = string p = phone/fax number d = date e = email address z = zip code f = file Format - needed to determine how the elements data should be formatted SEE "FORMATS" section Length (optional) - used to validate the length of the elements data first number is the minumum value; second number is the maximum value the "m" character is needed separate the minimum and maximum values example: 3m45 (min val is 3, max val is 45) */ function FormValidator(form_id,v_type) { this.form_obj = document.getElementById(form_id); this.v_type = v_type; // 'form' or a passed field name this.err_type = 'alert'; // 'alert' or 'dom' this.initials = 'jj_'; this.checked_array = new Array(); this.params = new Array(); this.el_type=''; this.el_name=''; this.el_params=''; this.el_value=''; // ############### error display variables ############### this.default_color = '#f1f293'; this.error_color = '#ff0000'; this.has_errors = false; this.error_string = ''; this.error_block = ''; this.errors = new Array(); this.error_ids = new Array(); this.pre_error_string = 'The following errors have been detected:' + "\n\n"; this.post_error_string = "\n\n" +'Please fix all errors and re-submit.'; this.errors_list = new Array(); this.errors_list[0] = ' was left empty.'; this.errors_list[1] = ' is formatted wrong.'; this.errors_list[2] = ' must be a whole number'; this.errors_list[3] = ' is less then the minimum value allowed.'; this.errors_list[4] = ' is less then the minimum characters required.'; this.errors_list[5] = ' is not a valid date'; this.errors_list[6] = ' was not checked'; this.errors_list[7] = ' was not selected'; this.errors_list[8] = ' is greater then the maximum value allowed.'; this.errors_list[9] = ' is greater then the maximum characters required.'; this.errors_list[10] = ' is not an accepted file format.'; // ############################################################## // ############### FORMATS - validation regular expressions ############### // (z) ZIP CODES this.zip_formats=new Array(); this.zip_formats[0] = /^\d{5}(\-\d{4})?$/; // xxxxx or xxxxx-xxxx this.zip_formats[1] = /^(\d{5}|\d{6})(\-\d{4})?$/; // xxxxx or xxxxx-xxxx or xxxxxx // (d) DATES this.date_formats=new Array(); this.date_formats[0] = /^\d{2}(\-|\/|\.)\d{2}\1\d{2}$/; // mm-dd-yy or mm/dd/yy or mm.dd.yy this.date_formats[1] = /^\d{2}(\-|\/|\.)\d{2}\1\d{4}$/; // mm-dd-yyyy or mm/dd/yyyy or mm.dd.yyyy this.date_formats[2] = /^\d{4}(\-|\/|\.)\d{2}\1\d{2}$/; // yyyy-mm-dd or yyyy/mm/dd or yyyy.mm.dd // (s) STRINGS this.string_formats=new Array(); this.string_formats[0] = /[*]/; // any alphanumeric character and the underscore this.string_formats[1] = /[^a-zA-Z\s]/; // any alphabetic character this.string_formats[2] = /[*]/; // any alphanumaeric character (generally for passwords) // (e) EMAILS this.email_formats=new Array(); //this.email_formats[0] = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; this.email_formats[0] = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i; // (n) NUMBERS this.number_formats=new Array(); this.number_formats[0] = /^\d+$/; // any whole number // (p) PHONE / FAX this.phone_formats=new Array(); this.phone_formats[0] = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/; //accepts phone number in both local format (eg. 02 1234 5678 or 123 123 4567) or international format (eg. +61 (0) 2 1234 5678 or +1 123 123 4567). It also accepts an optional extention of up to five digits prefixed by x or ext (eg. 123 123 4567 x89). this.phone_formats[1] = this.phone_formats[0]; // (f) FILENAME this.file_formats=new Array(); this.file_formats[0] = /\.(jpg)|(jpeg)$/i;// jpg or jpeg this.file_formats[1] = /\.(png)$/i;// png this.file_formats[2] = /\.(gif)$/i;// gif // ############################################################## this.validate = function () { var i,j,k,checked; //close dom alert window if open if (document.getElementById('alert_div')) { var alert_div = document.getElementById('alert_div'); document.body.removeChild(alert_div); } // revert field titles back to default color this.revert_fields(); if (this.v_type == 'form') { // validate entire form for (i=0;i 3) { min_max_vals = params_array[3]; if (min_max_vals.indexOf('m') != -1) { //ex. - 3m45 (min val is 3, max val is 45) len_array = min_max_vals.split('m'); this.params['min_length'] = len_array[0] - 0; //ex. - 3 or -3 this.params['max_length'] = len_array[1] - 0; } else { this.params['min_length'] = min_max_vals - 0; } } return true; } this.get_display_name = function(name) { var i,display_name='',tmp_array,spacer = ' '; tmp_array = name.split('_'); for (i=0;i -1) { sep = sep_array[i]; } } tmp_array = this.el_value.split(sep); switch (this.params['format']) { case '0' : if ((tmp_array[0]<1 || tmp_array[0]>12) || (tmp_array[1]<1 || tmp_array[1]>31)) { this.add_error(5); } break; case '1' : if ((tmp_array[0]<1 || tmp_array[0]>12) || (tmp_array[1]<1 || tmp_array[1]>31)) { this.add_error(5); } break; case '2' : if ((tmp_array[1]<1 || tmp_array[1]>12) || (tmp_array[2]<1 || tmp_array[2]>31)) { this.add_error(5); } break; } } } this.check_string = function() { if (this.string_formats[this.params['format']].test(this.el_value)) { this.add_error(1); } if (this.params['min_length'] != '' && this.el_value.length < this.params['min_length']) { this.add_error(4); } if (this.params['max_length'] != '' && this.el_value.length > this.params['max_length']) { this.add_error(9); } } this.check_email = function() { if (!this.email_formats[this.params['format']].test(this.el_value)) { this.add_error(1); } } this.check_number = function() { if (!this.number_formats[this.params['format']].test(this.el_value)) { this.add_error(2); } if (this.params['min_length'] != '' && this.el_value < this.params['min_length']) { this.add_error(3); } if (this.params['max_length'] != '' && this.el_value > this.params['max_length']) { this.add_error(8); } } this.check_phone = function() { if (!this.phone_formats[this.params['format']].test(this.el_value)) { this.add_error(1); } } } function close_alert() { document.getElementById('alert_div').style.display='none'; }