/* * Jeppesen form jQuery plug-in * * This is a jQuery plug-in for Jeppesen form validation and set for dependnecies * Developer: Pravesvuth Uparanukraw (Khem) * Requires: jQuery 1.3.1 or later * Version: 1.0 * Date: Mar 2009 * */ (function($) { $.JeppForm = { init: function(options) { $.JeppForm.options = $.extend($.JeppForm.options, options); $.JeppForm.bindValidate($.JeppForm.options.validate.element,$.JeppForm.options.validate.callback); }, options: { mandateClass: 'req', disabledClass: 'disabled', emailClass: 'email', numericClass: 'numeric', phoneClass: 'phone', creditcardClass:'credit', matchingClass: 'matching', dbMatchingClass: 'db', agreementClass: 'agreement', codeClass: 'code', codeRegex: null, ignoreClass: 'ignore', dateClass: 'date', showIndicator: true, hideDisabled: false, mandateApnStr: null, debug: false, highlightBind: false, msgBox: '#msgBox', errMsg: 'Error! Please review the form and submit again.', progressBox: '#progressBox', colors: { isRequired: '#FEE', isDBMatching: '#FEE', isEmail: '#FFE', isNumeric: '#EFE', isDate: '#FEF', isPhone: '#EFE', isCreditcard:'#EEF', isMatching: '#FFA', isAgreed: '#FEE' }, validate: { element: ':input[type=submit]', events: 'click', callback: function() { // do nothing } }, dateFormat: /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2}, \d{4}$/, dateFormatDefault: /^\d{1,2}\/\d{1,2}\/\d{4}$/, displayGeneralErrMSG: true }, defaultRules: { events: 'onDisable', action: 'disable', values: null, reverseAction: null }, validate: function(method) { var i; var className; var name; switch (method) { case "isRequired" : className = $.JeppForm.options.mandateClass; break; case "isEmail" : className = $.JeppForm.options.emailClass; break; case "isNumeric" : className = $.JeppForm.options.numericClass; break; case "isDate" : className = $.JeppForm.options.dateClass; break; case "isPhone" : className = $.JeppForm.options.phoneClass; break; case "isCreditcard" : className = $.JeppForm.options.creditcardClass; break; case "isMatching" : className = $.JeppForm.options.matchingClass; break; case "isDbMatching" : className = $.JeppForm.options.dbMatchingClass; break; case "isAgreed" : className = $.JeppForm.options.agreementClass; break; case "isCode" : className = $.JeppForm.options.codeClass; break; } var valid = true; var fields = $(":input:enabled."+className); for (i = 0; i< fields.length; i++) { var val = $(fields[i]).val(); switch (method) { case "isRequired" : if ($.isEmpty(val)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Required
"); valid = false; } break; case "isEmail" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val) && !$.isEmail(val)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid email address
"); valid = false; } break; case "isNumeric" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val) && !$.isNumeric(val)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid numeric value
"); valid = false; } break; case "isDate" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val) && !$.isDate(val)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Please enter a valid date
"); valid = false; } break; case "isPhone" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val) && !$.isPhone(val)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid phone number
"); valid = false; } break; case "isCreditcard" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val)) { name= $(fields[i]).attr("name"); if ($(fields[i]).hasClass("visa") && !$.isVisaCC(val)) { $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid Visa Number
"); valid = false; } else if ($(fields[i]).hasClass("mastercard") && !$.isMasterCardCC(val)) { $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid Master Card Number
"); valid = false; } else if ($(fields[i]).hasClass("amex") && !$.isAmexCC(val)) { $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid American Express Number
"); valid = false; } else if ($(fields[i]).hasClass("discover") && !$.isDiscoverCC(val)) { $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid Discover Number
"); valid = false; } else if (!$.isAnyCC(val)){ $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid Credit Card Number
"); valid = false; } } break; case "isMatching" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val)) { var matchingTarget = $(fields[i]).attr("matching"); var matchingValue = $("input[name="+matchingTarget+"]").val(); if (val != matchingValue) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ The "+$(fields[i]).attr("title")+" didn't match
"); valid = false; } } break; case "isDbMatching" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val)) { var ajaxUrl = $(fields[i]).attr("src"); if (!$.matchDB(val,ajaxUrl)) { name= $(fields[i]).attr("name"); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid "+$(fields[i]).attr("title")+" value
"); valid = false; } } break; case "isAgreed" : if (!fields[i].checked) { name= $(fields[i]).attr("name"); if ($.JeppForm.options.showIndicator) $(fields[i]).parent().append("
↑ If you agree, please check here to proceed.
"); valid = false; } break; case "isCode" : if ($(fields[i]).hasClass($.JeppForm.options.mandateClass) && ($.isEmpty(val))) { valid = false; } else if (!$.isEmpty(val) && !$.testRegex(val,$.JeppForm.options.codeRegex)) { name= $(fields[i]).attr("name"); $(fields[i]).css("background-color",$.JeppForm.options.colors[method]); if ($.JeppForm.options.showIndicator) $(fields[i]).after("
↑ Invalid code
"); valid = false; } break; } } if (method == "isRequired") { var legalSection = $("#legal"); var legalBox = $(":checkbox:enabled.legal"); name= $(legalBox).attr("name"); if (legalBox.length != 0 && !legalBox.is(":checked")) { valid=false; if ($.JeppForm.options.showIndicator) $(legalSection).after("
↑ Agree to the terms to proceed
"); } var checkBoxes = $(":checkbox:enabled."+className+"[title]"); var groups = new Array(); var lastTitle = null; var arrayIdx = 0; for (i = 0; i< checkBoxes.length; i++) { var title = $(checkBoxes[i]).attr("title"); if (title != lastTitle) groups[arrayIdx++] = title; lastTitle = title; } for (i = 0; i< groups.length; i++) { var groupValid = false; var reqBoxesChecked = $(":checkbox:checked:enabled."+className+"[title="+groups[i]+"]"); if (reqBoxesChecked.length > 0) { groupValid = true; } else { if ($.JeppForm.options.showIndicator) $(":checkbox:enabled."+className+"[title="+groups[i]+"]:last").parent() .append("
Please choose at least one
"); } if (!groupValid) valid=false; } var radioBoxes = $(":radio:enabled."+className); var radioGroup = new Array(); var lastRadio = null; var radioIdx = 0; for (i = 0; i < radioBoxes.length; i++) { var radio = $(radioBoxes[i]).attr("name"); if (radio != lastRadio) radioGroup[radioIdx++] = radio; lastRadio = radio; } for (i = 0; i < radioGroup.length; i++) { var reqRadioBoxesChecked = $(":radio:checked:enabled."+className+"[name="+radioGroup[i]+"]"); if (reqRadioBoxesChecked.length == 0) { valid = false; if ($.JeppForm.options.showIndicator) $(":radio:enabled."+className+"[name="+radioGroup[i]+"]:last").parent() .append("
Please choose
"); } } } return valid; }, bindValidate: function (element, callback) { $(element).bind($.JeppForm.options.validate.events, function() { var isRequiredFieldValid = true, isEmailFieldValid = true, isNumericFieldValid = true, isDateFieldValid = true, isPhoneValid = true, isCreditcardValid = true, isMatchingValid = true, isDbMatchingValid = true, isAgreementValid = true, isCodeValid = true; $(".indicator").remove(); $(":input:enabled").removeAttr("style"); isRequiredFieldValid = $.JeppForm.validate('isRequired'); isEmailFieldValid = $.JeppForm.validate('isEmail'); isNumericFieldValid = $.JeppForm.validate('isNumeric'); isDateFieldValid = $.JeppForm.validate('isDate'); isPhoneFieldValid = $.JeppForm.validate('isPhone'); isCreditcardValid = $.JeppForm.validate('isCreditcard'); isMatchingValid = $.JeppForm.validate('isMatching'); isDbMatchingValid = $.JeppForm.validate('isDbMatching'); isAgreementValid = $.JeppForm.validate('isAgreed'); isCodeValid = $.JeppForm.validate('isCode'); if (isRequiredFieldValid && isEmailFieldValid && isNumericFieldValid && isDateFieldValid && isPhoneFieldValid && isCreditcardValid && isMatchingValid && isDbMatchingValid && isAgreementValid && isCodeValid) { if ($.JeppForm.options.msgBox != null) { $($.JeppForm.options.msgBox).removeClass('cautionBox').empty(); } if (typeof callback === "function") callback(); } else { if ($.JeppForm.options.msgBox != null) { $($.JeppForm.options.msgBox).empty().addClass('cautionBox'); if ($.JeppForm.options.displayGeneralErrMSG) { $($.JeppForm.options.msgBox).append( ""+ $.JeppForm.options.errMsg+ ""+ "Missing required fields! Red fields are required."+ ""+ "Invalid email! Yellow fields require a valid email."+ ""+ "Invalid numeric! Green fields require a valid numeric"+ ""+ "Invalid date! Purple fields require a valid date."+ ""+ "Invalid numeric! Green fields require a valid phone number"+ ""+ "Invalid numeric! Green fields require a valid credit card number"+ ""+ "Invalid code!"+ ""+ "Values are not matching pleaser try again"+ ""+ "Invalid value"+ " 0) { if ($(this).hasClass($.JeppForm.options.numericClass)) { if (!$.isNumeric(val)) { $(this).css("background-color",$.JeppForm.options.colors["isNumeric"]); if ($.JeppForm.options.showIndicator) $(this).after("
↑ Invalid numeric value
"); } else { $("#for_"+name).remove(); $(this).removeAttr("style"); } } else if ($(this).hasClass($.JeppForm.options.emailClass)) { if (!$.isEmail(val)) { $(this).css("background-color",$.JeppForm.options.colors["isEmail"]); if ($.JeppForm.options.showIndicator) $(this).after("
↑ Invalid email
"); } else { $("#for_"+name).remove(); $(this).removeAttr("style"); } } } }); }, $.fn.toggle = function(callback) { var options = $.JeppForm.options; var _this = $(this); var inputs = $(this).filter(':input'); if (inputs.attr('disabled') == false) { _this.disable(); } else { _this.enable(); } inputs.trigger('onToggle'); if (options.debug) console.log('toogle'); if (typeof callback === "function") callback(); return $(this); }; $.fn.disable = function(callback) { var options = $.JeppForm.options; var input = $(this).filter(':input'); input.attr('disabled',true); input.addClass(options.disabledClass); $(this).addClass(options.disabledClass); $(this).unmandate(); $(this).trigger('onDisable'); if (options.hideDisabled) $(this).hide(); if (options.debug) console.log('disable'); if (typeof callback === "function") callback(); return $(this); }; $.fn.enable = function(callback) { var options = $.JeppForm.options; var inputs = $(this).filter(':input'); inputs.removeAttr('disabled'); inputs.removeClass(options.disabledClass); $(this).removeClass(options.disabledClass); $(this).trigger('onEnable'); if (options.hideDisabled) $(this).show(); if (options.debug) console.log('enable'); if (typeof callback === "function") callback(); return $(this); }; $.fn.mandate = function(callback) { var options = $.JeppForm.options; var inputs = $(this).filter(':input:not(.'+options.ignoreClass+')'); var labels = $(this).filter('label'); inputs.addClass(options.mandateClass); inputs.each(function(i) { var input = $(inputs[i]); if (options.mandateApnStr != null && input.attr('name').indexOf(options.mandateApnStr) == -1) { input.attr('name',input.attr('name') + options.mandateApnStr); } }); if ($("em.mandatelabel",labels).length == 0) labels.prepend(''); $(this).trigger('onMandate'); if (options.debug) console.log('mandate'); if (typeof callback === "function") if (typeof callback === "function") callback(); return $(this); }; $.fn.mandateAndBind = function(callback) { $(this).mandate(); $(this).setDependency(this,{events: 'onEnable', action: 'mandate'}); if (typeof callback === "function") callback(); return $(this); }; $.fn.unmandate = function(callback) { var options = $.JeppForm.options; var inputs = $(this).filter(':input'); var labels = $(this).filter('label'); inputs.removeClass(options.mandateClass); inputs.each(function(i) { var input = $(inputs[i]); if (options.mandateApnStr != null && input.attr('name').indexOf(options.mandateApnStr) != -1) { input.attr('name', input.attr('name').substr(0,input.attr('name').indexOf(options.mandateApnStr)) ); } }); $("em.mandatelabel",labels).remove(); $(this).trigger('onUnmandate'); if (options.debug) console.log('unmandate'); if (typeof callback === "function") callback(); return $(this); }; $.fn.unmandateAndUnbind = function(callback) { $(this).unmandate(); $(this).releaseDependency(this); if (typeof callback === "function") callback(); return $(this); }; $.fn.format = function(format, callback) { var options = $.JeppForm.options; var inputs = $(this).filter(':input'); if (format.isEmail) inputs.addClass(options.emailClass); if (format.isNumeric) inputs.addClass(options.numericClass); if (format.isDate) inputs.addClass(options.dateClass); if (typeof callback === "function") callback(); return $(this); }; $.isEmpty = function (str) { return (str == null) || (str == undefined) || (str.length == 0) || /^\s.$/.test(str) ; }; $.isEmail = function (str) { if($.isEmpty(str)) return false; var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i; return re.test(str); }; $.isNumeric = function (str) { if($.isEmpty(str)) return false; if (isNaN(str)) return false; return true; }; $.isDate = function (str) { if($.isEmpty(str)) return false; return $.JeppForm.options.dateFormat.test(str); }; $.isPhone = function (str) { if($.isEmpty(str)) return false; var re = /^(\+\d)*\s*(\(\d*\)\s*)*[\s\d-.]{5}(ext|ext.|extention)*[\s\d-.]*$/; return re.test(str); }; $.isVisaCC = function (str) { if($.isEmpty(str)) return false; var re = /^4[0-9]{12}(?:[0-9]{3})?$/; return re.test(str); }; $.isMasterCardCC = function (str) { if($.isEmpty(str)) return false; var re = /^5[1-5][0-9]{14}$/; return re.test(str); }; $.isAmexCC = function (str) { if($.isEmpty(str)) return false; var re = /^3[47][0-9]{13}$/; return re.test(str); }; $.isDiscoverCC = function (str) { if($.isEmpty(str)) return false; var re = /^6(?:011|5[0-9]{2})[0-9]{12}$/; return re.test(str); }; $.isAnyCC = function (str) { if($.isEmpty(str)) return false; var re = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/; return re.test(str); }; $.testRegex = function (str, regex) { if (regex != null) { return regex.test(str); } else { return false; } }; $.matchDB = function (str, ajax_url) { if($.isEmpty(str)) return false; _res = false; $.ajax({ async: false, url: ajax_url, data: {data:str}, success: function (data) { res = $("result",data).text(); _res = $.trim(res).indexOf("true") != -1; } }); return _res; }; $.getIntlDialingCode = function (country) { switch (country) { case "US" : return "1"; case "AF" : return "93"; case "AL" : return "335"; case "DZ" : return "213"; case "AS" : return "1-684"; case "AD" : return "376"; case "AO" : return "244"; case "AI" : return "1-264"; case "AQ" : return "672"; case "AG" : return "1-268"; case "AR" : return "54"; case "AM" : return "374"; case "AW" : return "297"; case "AU" : return "61"; case "AT" : return "43"; case "AZ" : return "994"; case "BS" : return "1-242"; case "BH" : return "973"; case "BB" : return "1-246"; case "BY" : return "375"; case "BE" : return "32"; case "BZ" : return "501"; case "BJ" : return "229"; case "BM" : return "1-441"; case "BT" : return "975"; case "BO" : return "591"; case "BA" : return "387"; case "BW" : return "267"; case "BV" : return ""; case "BR" : return ""; case "IO" : return ""; case "BN" : return ""; case "BG" : return ""; case "BF" : return ""; case "BI" : return ""; case "KH" : return ""; case "CM" : return ""; case "CA" : return ""; case "XC" : return ""; case "CV" : return ""; case "KY" : return ""; case "CF" : return ""; case "TD" : return ""; case "XG" : return ""; case "CL" : return ""; case "CN" : return ""; case "CC" : return ""; case "CO" : return ""; case "KM" : return ""; case "CD" : return ""; case "CG" : return ""; case "CK" : return ""; case "CR" : return ""; case "HR" : return ""; case "CU" : return ""; case "CY" : return ""; case "CZ" : return ""; case "DK" : return ""; case "DJ" : return ""; case "DM" : return ""; case "DO" : return ""; case "TP" : return ""; case "EC" : return ""; case "EG" : return ""; case "SV" : return ""; case "GQ" : return ""; case "ER" : return ""; case "EE" : return ""; case "ET" : return ""; case "FK" : return ""; case "FO" : return ""; case "FJ" : return ""; case "FI" : return ""; case "FR" : return ""; case "GF" : return ""; case "PF" : return ""; case "TF" : return ""; case "GA" : return ""; case "GM" : return ""; case "GE" : return ""; case "DE" : return ""; case "GH" : return ""; case "GI" : return ""; case "GR" : return ""; case "GL" : return ""; case "GD" : return ""; case "GP" : return ""; case "GU" : return ""; case "GT" : return ""; case "GN" : return ""; case "GW" : return ""; case "GY" : return ""; case "HT" : return ""; case "HM" : return ""; case "VA" : return ""; case "HN" : return ""; case "HK" : return ""; case "HU" : return ""; case "IS" : return ""; case "IN" : return ""; case "ID" : return ""; case "IR" : return ""; case "IQ" : return ""; case "IE" : return ""; case "IL" : return ""; case "IT" : return ""; case "CI" : return ""; case "JM" : return ""; case "JP" : return ""; case "JO" : return ""; case "KZ" : return ""; case "KE" : return ""; case "KI" : return ""; case "KP" : return ""; case "KR" : return ""; case "KW" : return ""; case "KG" : return ""; case "LA" : return ""; case "LV" : return ""; case "LB" : return ""; case "LS" : return ""; case "LR" : return ""; case "LY" : return ""; case "LI" : return ""; case "LT" : return ""; case "LU" : return ""; case "MO" : return ""; case "MK" : return ""; case "MG" : return ""; case "MW" : return ""; case "MY" : return ""; case "MV" : return ""; case "ML" : return ""; case "MT" : return ""; case "MH" : return ""; case "MQ" : return ""; case "MR" : return ""; case "MU" : return ""; case "YT" : return ""; case "MX" : return ""; case "FM" : return ""; case "MD" : return ""; case "MC" : return ""; case "MN" : return ""; case "MS" : return ""; case "MA" : return ""; case "MZ" : return ""; case "MM" : return ""; case "NA" : return ""; case "NR" : return ""; case "NP" : return ""; case "NL" : return ""; case "AN" : return ""; case "NC" : return ""; case "NZ" : return ""; case "NI" : return ""; case "NE" : return ""; case "NG" : return ""; case "NU" : return ""; case "NF" : return ""; case "MP" : return ""; case "NO" : return ""; case "OM" : return ""; case "PK" : return ""; case "PW" : return ""; case "PS" : return ""; case "PA" : return ""; case "PG" : return ""; case "PY" : return ""; case "BD" : return ""; case "PE" : return ""; case "PH" : return ""; case "PN" : return ""; case "PL" : return ""; case "PT" : return ""; case "PR" : return ""; case "QA" : return ""; case "RE" : return ""; case "RO" : return ""; case "RU" : return ""; case "RW" : return ""; case "SH" : return ""; case "KN" : return ""; case "LC" : return ""; case "PM" : return ""; case "VC" : return ""; case "WS" : return ""; case "SM" : return ""; case "ST" : return ""; case "SA" : return ""; case "SN" : return ""; case "YU" : return ""; case "SC" : return ""; case "SL" : return ""; case "SG" : return ""; case "SK" : return ""; case "SI" : return ""; case "SB" : return ""; case "SO" : return ""; case "ZA" : return ""; case "GS" : return ""; case "ES" : return ""; case "LK" : return ""; case "SD" : return ""; case "SR" : return ""; case "SJ" : return ""; case "SZ" : return ""; case "SE" : return ""; case "CH" : return ""; case "SY" : return ""; case "TW" : return ""; case "TJ" : return ""; case "TZ" : return ""; case "TH" : return "66"; case "TG" : return ""; case "TK" : return ""; case "TO" : return ""; case "TT" : return ""; case "TN" : return ""; case "TR" : return ""; case "TM" : return ""; case "TC" : return ""; case "TV" : return ""; case "UG" : return ""; case "UA" : return ""; case "AE" : return ""; case "GB" : return ""; case "UM" : return ""; case "UY" : return ""; case "UZ" : return ""; case "VU" : return ""; case "VE" : return ""; case "VN" : return ""; case "VG" : return ""; case "VI" : return ""; case "WF" : return ""; case "EH" : return ""; case "YE" : return ""; case "ZM" : return ""; case "ZW" : return ""; default: return ""; } }; })(jQuery);