var validate = {location:'after', rules:{ email:{ regex:"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", error_message:"invalid email address." }, digit:{ regex:"^[0-9]+$", error_message:"must be a number." }, varchar:{ regex:".+", error_message:"must contain number or characters." }, min_3_char:{ regex:".{3,}", error_message:"must have at least 3 characters." }, min_5_char:{ regex:".{5,}", error_message:"must have at least 5 characters." }, phone_number:{ regex:"^[0-9]{3}[\.\-]?[0-9]{3}[\.\-]?[0-9]{4}$|^[0-9]{4,5}$", error_message:"must be valid extension or 10 digit phone number" }, file:{ regex:"[0-9a-zA-Z]+", error_message:"upload file is required." }, emplid:{ regex:"^[0-9]{7}$", error_message:"incorrect UH ID. example 0123456" }, zipcode:{ regex:"^[0-9]{5}$", error_message:"must be a valid 5 digit zipcode." }, selected:{ regex:"^.+$", error_message:"please select from the dropdown." }, date_yyyy_mm_dd:{ regex:"^[0-9]{4}(\-|\.|\/)[0-9]{1,2}(\-|\.|\/)[0-9]{1,2}$", error_message:"must be YYYY-MM-DD" }, date_mm_dd_yyyy:{ regex:"^[0-9]{1,2}(\-|\.|\/)[0-9]{1,2}(\-|\.|\/)[0-9]{4}$", error_message:"must be MM-DD-YYYY" }, mac_address:{ regex:"^([0-9a-fA-F]{2}([:-])?){6}$", error_message:"MAC address entered is not valid, Examples: aa-aa-aa-aa-aa-aa" }, hourtime:{ regex:"^(0?[1-9]|1[0-2]):([0-5][0-9])\s?([aApP][mM]?)$", error_message:"time is invalid, Examples: HH:MM am" } }, required:{} , add_error: function(obj,message){ $(obj).addClass('input-error'); $(obj).parent().addClass('has-error'); var e_id = obj.attr('id')+'-error'; var e_class = 'error-inline'; //see if there is a label for this input var e_label = $('label[for='+obj.attr('name')+']').text(); var e_message = ''+e_label+' '+message+''; if (this.location == 'after'){ $(obj).after(e_message); }else{ $(obj).before(e_message); } }, remove_error: function(obj){ $(obj).removeClass('input-error'); $(obj).parent().removeClass('has-error'); $('#'+obj.attr('id')+'-error').remove(); }, is: function(type, x){ var element_data = ''; element_data = $(x).val(); element_id = $(x).attr('id'); if (this.rules[type]){ if (this.required[element_id]){ if (!element_data.search(this.rules[type].regex)){ $(x).removeClass('input-error'); this.remove_error(x); return true; }else{ this.remove_error(x); this.add_error(x, this.rules[type].error_message); return false; } }else{ if (!element_data.search(this.rules[type].regex) || !element_data.search(/^$/)){ $(x).removeClass('input-error'); this.remove_error(x); return true; }else{ this.remove_error(x); this.add_error(x, this.rules[type].error_message); return false; } } }else{ alert ('validation rule does not exist'); return false; } }, compare:function (x,y){ //compare two text fields try{ if (x.value != y.value || x.value.length < 3){ $(x).addClass('input-error'); $(y).addClass('input-error'); this.remove_error(x); this.add_error(x,'does not match or too short'); }else{ $(x).removeClass('input-error'); $(y).removeClass('input-error'); this.remove_error(x); return true; } }catch(e){ return false; } } }; function validate_form(){ var errors =0; validate.is('min_3_char',document.getElementById('name')) ? 0: errors++; validate.is('email',document.getElementById('email')) ? 0: errors++; if (errors > 0){ document.getElementById('name').focus(); return false; }else{ return true; } }