//----------------------------------------------------------------------------- // value // - this function extends jQuery // - returns the value of form elements based on A single or B group type: // // A) single : ie - return input value // B) group : ie - return selected radio value //----------------------------------------------------------------------------- $.fn.value = function() { if( (this.attr("type") == 'radio' || this.attr("type") == 'checkbox') && typeof(this.attr('name')) != 'undefined'){ var value = $("input[name="+this.attr('name')+"]:checked").val() || false; if(value) return value; else return ''; } else { try { return convertFrenchCharsToEnglish (this.val()); } catch (err) { return this.val(); } } }; $.fn.summary = function() { //array of selected items, run hasError on each item if(this.length > 1){ for(var i = 0; i < this.length; i++){ $(this[i]).summary(); } } //if selected item does not have class summary, search inside it for //elements that have it; run summary on anything found else if(!this.hasClass('qk-summary')){ var found_summaries = this.find(".qk-summary"); if(found_summaries.length) { $(found_summaries).summary(); } } //split class string and look through each class //if class matches a field and has a value that isn't undefined //add it to the display string else { var classes = $(this).attr('class').split(' '); var display_string = ''; for (var i = 0; i < classes.length; i++) { if(typeof($("#"+classes[i]+"_id").val()) != 'undefined'){ if( $("#"+classes[i]+"_id").prop("tagName").toLowerCase() == 'select' && $("#"+classes[i]+"_id").val() != ""){ display_string+= " " + $("#"+classes[i]+"_id").find("option:selected").html(); } else if($("#"+classes[i]+"_id").attr("type") == 'radio'){ display_string+= " " + $("input[name="+classes[i]+"]:checked").val(); } else{ display_string+= " " + $("#"+classes[i]+"_id").val(); } } } if(display_string.match(/^\s$/)){ $(this).hide(); } else{ $(this).show(); } //alert(display_string); $(this).find("span").html(display_string); } }; $.fn.valid_bak = function() { //function valid (array_of_form_fields){ var valid = true; //array_of_form_fields.each( function(){ this.each( function(){ //console.log($(this).attr("name")); var field_name, field_value; // skip nameless and hidden fields if (typeof(fields[$(this).attr("name")]) == "undefined" || $(this).is(":hidden")){ return; }else{ field_value = $(this).value(); field_name = $(this).attr("name"); } console.log("value :" + field_value); var field_constraints = fields[field_name]; //loop through constraint names main_loop: for(var key in field_constraints){ // loop through all dependencies // go to next constraint on fail if(field_constraints[key].deps){ for( var dep_field in field_constraints[key].deps ){ for( var dep in field_constraints[key].deps[dep_field] ){ //skip the current constraint if it doesn't pass // its dependencies var params = field_constraints[key].deps[dep_field][dep]; if( !constraints[dep]( $("input[name="+dep_field+"], select[name="+dep_field+"]").value(), params.slice(0)) ){ continue main_loop; } } } } // Runs validation function on form fields //run the constraint, passing value from this and params from service var params; if(field_constraints[key].params){ params = field_constraints[key].params; }else{ params = [""]; } // Entering this if statement means the field is invalid console.log("key>>" + key+ " Field val>>"+field_name); var field_is_valid = constraints[key]($(this).value(), params.slice(0)); valid = field_is_valid; //alert(field_is_valid); if( !field_is_valid ) { var error = ( typeof content[field_name][$('#lang_id').val()].errors[key] == 'undefined')? 'default' : key; $(this).show_error(field_is_valid, content[field_name][$('#lang_id').val()].errors[error]); return false; } } }); if (valid)$(this).show_error(true); return valid; } $.fn.valid = function() { //array of selected items, run valid() on each item debug("Validating..."); // "this" is an array of elements inside a given form if(this.length > 1){ debug("\tMultiple items found, looping through each."); var error_flag = false; for(var i = 0; i < this.length; i++){ debug("Loop: "+i); if( !$(this[i]).valid() ) error_flag = true; } if(error_flag) return false; else return true; } //selected item is not form field //look for form fields inside and valid() them else if( this.prop("tagName") && !this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) ){ debug("\tNot a input field, searching for fields inside"); var found_fields = this.find('input, select, textarea'); if(found_fields.length) { return $(found_fields).valid(); } else { //return true, because nothing was found return true; } } //if selected is a form field //AND make sure there is an entry in the field response else if( this.prop("tagName") && this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) && typeof(fields[this.attr("name")]) != "undefined"){ debug("\tInput field found: " + this.attr("name")); var field_name = this.attr("name"); var field_constraints = fields[field_name]; //loop through constraint names main_loop: for(var key in field_constraints){ debug('\t\tConstraint: '+ key); // do a dependency check if(field_constraints[key].deps){ debug('\t\t\tDependencies'); for( var dep_field in field_constraints[key].deps ){ debug('\t\t\tField: '+dep_field); for( var dep in field_constraints[key].deps[dep_field] ){ debug('\t\t\tConstraint: '+dep); //skip the current constraint if it doesn't pass // its dependencies var params = field_constraints[key].deps[dep_field][dep]; if( !constraints[dep]( $("input[name="+dep_field+"], select[name="+dep_field+"]").value(), params.slice(0)) ){ debug('\t\t\tFailed'); continue main_loop; } debug('\t\t\tPassed'); } } } //run the constraint, passing value from this and params from //service var params; if(field_constraints[key].params){ params = field_constraints[key].params; }else{ params = [""]; } if( typeof (constraints[key]) != 'undefined' && !constraints[key](this.value(), params.slice(0)) ){ var error = ( typeof content[field_name][$('#lang_id').val()].errors[key] == 'undefined')? 'default' : key; this.show_error(false, content[field_name][$('#lang_id').val()].errors[error]); debug('\t\tFailed'); return false; } debug('\t\tPassed'); } //clear errors this.show_error(true); return true; } else if(typeof(fields[this.attr("name")]) == "undefined"){ debug('No validation exists for: '+this.attr("name")); return false; } else{ debug('Mystery (returning true for now)'); //no validation found //or not a tag //go to error page? //return true for now return true; } }; $.fn.show_error = function(valid, error_message) { var field_name = this.attr("name"); if (!valid && typeof(error_message) == "undefined"){ error_message = " "; } // show error if valid is false if(!valid){ if(this.attr('type') == 'radio' || this.attr('type') == 'checkbox'){ $("input[name="+field_name+"]").addClass("qk-error"); } else this.addClass("qk-error"); $("."+field_name+".qk-message span").html(error_message); $("."+field_name+".qk-message").show(); $("."+field_name+".qk-parent").addClass('qk-error'); $("."+field_name+".qk-parent").removeClass("qk-valid"); } // hide error if valid is true else{ if(this.attr('type') == 'radio' || this.attr('type') == 'checkbox'){ $("input[name="+field_name+"]").removeClass("qk-error"); } else this.removeClass("qk-error"); $("."+field_name+".qk-message span").html(''); $("."+field_name+".qk-message").hide(); if($("."+field_name+".qk-parent").find('.qk-error').length == 0){ $("."+field_name+".qk-parent").removeClass('qk-error'); $("."+field_name+".qk-parent").addClass("qk-valid"); } } }; function debug(string){ if(typeof(DEBUG) != 'undefined' && DEBUG == true){ console.log(string); } } if(typeof convertFrenchCharsToEnglish == 'undefined') { convertFrenchCharsToEnglish = function (text) { return text.replace(/[\u00A0-\u2666<>\&]/g, function(c) { return (convertFrenchCharsToEnglish.entityTable[c.charCodeAt(0)] || '#'+c.charCodeAt(0)); }); }; // all HTML4 entities as defined here: http://www.w3.org/TR/html4/sgml/entities.html convertFrenchCharsToEnglish.entityTable = { /*''*/ 192 : 'A', /*''*/ 193 : 'A', /*''*/ 194 : 'A', /*''*/ 196 : 'A', /*''*/ 199 : 'C', /*''*/ 200 : 'E', /*''*/ 201 : 'E', /*''*/ 202 : 'E', /*''*/ 203 : 'E', /*''*/ 204 : 'I', /*''*/ 205 : 'I', /*''*/ 206 : 'I', /*''*/ 207 : 'I', /*''*/ 210 : 'O', /*''*/ 211 : 'O', /*''*/ 212 : 'O', /*''*/ 217 : 'U', /*''*/ 218 : 'U', /*''*/ 219 : 'U', /*''*/ 220 : 'U', /*''*/ 224 : 'a', /*''*/ 225 : 'a', /*''*/ 226 : 'a', /*''*/ 228 : 'a', /*''*/ 231 : 'c', /*''*/ 232 : 'e', /*''*/ 233 : 'e', /*''*/ 234 : 'e', /*''*/ 235 : 'e', /*''*/ 236 : 'i', /*''*/ 237 : 'i', /*''*/ 238 : 'i', /*''*/ 239 : 'i', /*''*/ 242 : 'o', /*''*/ 243 : 'o', /*''*/ 244 : 'o', /*''*/ 249 : 'u', /*''*/ 250 : 'u', /*''*/ 251 : 'u', /*''*/ 252 : 'u', /*''*/ 255 : 'y', /*''*/ 376 : 'Y' }; }