-
Notifications
You must be signed in to change notification settings - Fork 0
/
front-end-validation.min.js
5 lines (5 loc) · 8.1 KB
/
front-end-validation.min.js
1
2
3
4
5
var creditCard=function(e){var a="undefined"!=typeof e&&"undefined"!=typeof e.errorClass?e.errorClass:"has-error",s="undefined"!=typeof e&&"undefined"!=typeof e.successClass?e.successClass:"has-success",r=this;$(document).ready(function(){r.utils=new utils}),this.validateNumber=function(e){var t=$("#card-type"),l=$(e);if(l.removeClass("has-error").removeClass("has-success"),t.removeClass("visa").removeClass("mc").removeClass("amex").text(""),$(e).val().length<2)return void t.removeClass("card").addClass("cards");var n=this.getCardType($(e).val()),i=$(e).val();switch(i=i.replace(/\s|-/g,""),n){case"Visa":t.removeClass("cards").addClass("card visa"),l.val(this.formatCreditCard(i,n));break;case"Mastercard":t.removeClass("cards").addClass("card mc"),l.val(this.formatCreditCard(i,n));break;case"AMEX":t.removeClass("cards").addClass("card amex"),l.val(this.formatCreditCard(i,n));break;default:t.removeClass("cards"),l.attr("placeholder","Visa, MC, or Amex"),r.utils.addValidationClass(e,a)}var d="AMEX"==n?15:16;return!(i.length>=d)||(this.validateCardCheckSum(l.val())?(r.utils.addValidationClass(e,s),!0):(r.utils.addValidationClass(e,a),!1))},this.formatCreditCard=function(e,a){var s=e.substring(0,4),r="",t="",l="";return e.length>=4&&(r="AMEX"==a?" - "+e.substring(4,e.length<=10?e.length:10):" - "+e.substring(4,e.length<=8?e.length:8)),e.length>=("AMEX"==a?10:8)&&(t="AMEX"==a?" - "+e.substring(10,e.length<=15?e.length:15):" - "+e.substring(8,e.length<=12?e.length:12)),e.length>=12&&"AMEX"!=a&&(l="AMEX"==a?"":" - "+e.substring(12,e.length<=16?e.length:16)),s+r+t+l},this.getCardType=function(e){if(e){var a=new RegExp("^4");return null!=e.match(a)?"Visa":(a=new RegExp("^5[1-5]"),null!=e.match(a)?"Mastercard":(a=new RegExp("^3[47]"),null!=e.match(a)?"AMEX":(a=new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)"),null!=e.match(a)?"Discover":(a=new RegExp("^36"),null!=e.match(a)?"Diners":(a=new RegExp("^30[0-5]"),null!=e.match(a)?"Diners - Carte Blanche":(a=new RegExp("^35(2[89]|[3-8][0-9])"),null!=e.match(a)?"JCB":(a=new RegExp("^(4026|417500|4508|4844|491(3|7))"),null!=e.match(a)?"Visa Electron":"")))))))}},this.validateCardCheckSum=function(e){e=e.replace(/\s|-/g,"");var a=e.split("").reverse().join(""),s="";for(i=0;i<a.length;i++){var r=parseInt(a.charAt(i),10);i%2!=0&&(r*=2),s+=r}var t=0;for(i=0;i<s.length;i++)r=parseInt(s.charAt(i),10),t+=r;return 0!=t&&t%10==0}};
var eventListeners=function(i){var e="undefined"!=typeof i&&"undefined"!=typeof i.arrNumericalFields?i.arrNumericalFields:['input[name="credit_card_number"]','input[name="cvv"]','input[validation="int"]'],t=this,n=!1;$(document).ready(function(){t.utils=new utils,t.validate=new validate}),this.validateField=function(){$(document).on("blur","form input",function(i){t.validate.field(this,"filter")})},$(document).on("keyup","form input.keyup-validation",function(i){t.utils.isDeleteOrBackspaceKey(i)||t.validate.field(this,"filter")}),this.validateSelect=function(){$(document).on("change","form select",function(){t.validate.field(this)})},this.validateNumerical=function(){$.each(e,function(i,e){$(document).on("keyPress",e,function(i){return t.utils.isDeleteOrBackspaceKey(i)||t.utils.isNumericalKey(i)})})},this.cvvInfo=function(){$(document).on("click","a.cvv-popover",function(i){i.preventDefault(),$(this).popover()})},this.validateForm=function(){$(document).on("submit","form",function(i){$(this).hasClass("modal-form")||$(this).hasClass("no-submit-disable")||(t.validate.form(this)?(n=!1,$("#disable-page-processing").fadeIn(),$(this).find("button").last().prop("disabled",!0).text("Processing..."),$(this).find('input[type="submit"]').prop("disabled",!0)):(n=!0,i.preventDefault(),$(this).find("button").last().prop("disabled",!1),$(this).find('input[type="submit"]').prop("disabled",!1),$("#disable-page-processing").hide()))})}};
var frontEndValidation=function(e){var t=this;$(document).ready(function(){t.initiateEventListeners()}),this.initiateEventListeners=function(){var t=new eventListeners(e);t.validateField(),t.validateSelect(),t.validateNumerical(),t.cvvInfo(),t.validateForm()}};
var utils=function(){this.isDeleteOrBackspaceKey=function(e){var s="number"==typeof e.which?e.which:e.keyCode;return 8==s||46==s},this.isNumericalKey=function(e){var s="number"==typeof e.which?e.which:e.keyCode;return s>=48&&s<=57},this.fieldNameToString=function(e){if("undefined"!=typeof e)return e=e.replace(/_/g," "),e=e.replace(/\[]/g,""),this.ucwords(e)},this.ucwords=function(e){return(e+"").replace(/^([a-z])|\s+([a-z])/g,function(e){return e.toUpperCase()})},this.addValidationClass=function(e,s,t,r){$(e).removeClass(t),$(e).removeClass(r),$(e).closest(".input-group").removeClass(t),$(e).closest(".input-group").removeClass(r),$(e).addClass(s),$(e).closest(".input-group").addClass(s)}};
var validate=function(e){var t="undefined"!=typeof e&&"undefined"!=typeof e.errorClass?e.errorClass:"has-error",a="undefined"!=typeof e&&"undefined"!=typeof e.successClass?e.successClass:"has-success",r=this,i="";$(document).ready(function(){r.utils=new utils,r.card=new creditCard(e)}),this.form=function(e){var a="",i=$(e).find("[validation],[required]"),d=[];if($.each(i,function(e,i){a=r.field(i),a&&(d.push(a),r.utils.addValidationClass(i,t))}),d.length){var n=$("#bs-modal"),s=n.find(".modal-title"),l=n.find(".modal-body"),c="";s.html("Please correct input");for(var u=0;u<d.length;u++)c+="<li>"+d[u]+"</li>";return l.html(c),n.modal("show").on("hidden.bs.modal",function(){s.text("Message"),l.text("")}),!1}return!a},this.field=function(e,d){var d="undefined"!=typeof d&&d,n="",s=$(e).attr("validation");!s&&$(e).attr("required")&&"credit_card_number"==$(e).attr("name")&&(s="credit_card_number"),!s&&$(e).attr("required")&&"cvv"==$(e).attr("name")&&(s="cvv"),!s&&$(e).attr("type")&&(s=$(e).attr("type"));var l=$(e).val();if("hidden"!=$(e).attr("type")){var c=$('label[for="'+$(e).attr("name")+'"]').text();if(c=c.replace(/\*/g,""),c||(c=$(e).attr("title")&&!$(e).hasClass("has-error")?$(e).attr("title"):r.utils.fieldNameToString($(e).attr("name"))),$(e).attr("required")&&(!l||"checkbox"==$(e).attr("type")&&!$(e).is(":checked")))return r.utils.addValidationClass(e,t),'"'+c+'" is a required field';if(l){var u="";switch(s){case"email":u=/^(([^<>()[\]\\.,;:\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=u.test(l)?a:t,n=u.test(l)?"":"The email address appears to be invalid";break;case"url":u=/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>#%",\{}\\|\\\^\[\]`]+)?$/,i=u.test(l)?a:t,n=u.test(l)?"":"The URL appears to be invalid";break;case"domain":u=/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/,i=u.test(l)?a:t,n=u.test(l)?"":"The domain name appears to be invalid";break;case"ip":u=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,i=u.test(l)?a:t,n=u.test(l)?"":"The IP appears to be invalid";break;case"number":case"int":d&&(l=l.replace(/\D/,""),$(e).val(l)),i=l%1===0?a:t,n=l%1===0?"":"The value does not appear to be a valid number";break;case"float":i=isNaN(l)||l.toString().indexOf(".")==-1?t:a,n=isNaN(l)||l.toString().indexOf(".")==-1?"The number is not a float/decimal":"";break;case"date":u=/(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12][0-9]|3[01])\/(?:19|20\d{2})/,i=u.test(l)?a:t,n=u.test(l)?"":"Invalid date (required format: mm/dd/yyyy)";break;case"credit_card_number":var o=r.card.validateNumber(e);i=o?a:t,n=o?"":"Invalid Credit Card Number";break;case"cvv":var m=r.card.getCardType($('input[name="credit_card_number"]').val());m=m?m:r.card.getCardType($('input[name="cc_number"]').val()),"AMEX"==m?(i=4==l.length?a:t,n=4==l.length?"":"AMEX CVV must be a 4 digit security code"):m&&(i=3==l.length?a:t,n=3==l.length?"":"Card CVV must be a 3 digit security code")}var h=$(e).attr("minlength")&&l.length<$(e).attr("minlength");return n||(i=h?t:"#00b9e4",n=h?"Minimum length required for "+c+" is: "+$(e).attr("minlength"):""),h=$(e).attr("maxlength")&&l.length>$(e).attr("maxlength"),n||(i=h?t:a,n=h?"Maximum length required for "+c+" is: "+$(e).attr("maxlength"):""),i&&r.utils.addValidationClass(e,i,t,a),n}}}};