-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_functions.min.js
More file actions
11 lines (11 loc) · 5.44 KB
/
form_functions.min.js
File metadata and controls
11 lines (11 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
/*
* Form-Functions - v1.6.1
* Help functions for processing form errors
* http://highgate-creative.com
*
* Made by
* Sean Zellmer <sean@lejeunerenard.com>
* Bradford Cathey <bradc@highgate-creative.com>
* Under MIT License
*/
!function(a,b,c,d){function e(b,d){var e={};void 0!==d?(e.element=b,e.prefix=d):b&&(e.element=b),c(".errors_above").css("display","none");var f;f=c(e.element?"#"+c(e.element).attr("id")+" label[for]":"label[for]"),f.not(".static_label").each(function(){c(this).removeClass("error_label");var b=i(this,e),d=a.getElementById(this.htmlFor);0!==c('label.static_label[for="'+c(this).attr("for")+'"]').length&&(b=""),d&&(void 0!==d.setCustomValidity?d.setCustomValidity(""):c(d).removeClass("error")),c(this).text(b)})}function f(a){"string"==typeof a&&"["===a[0]&&(a=JSON.parse(a)),"string"==typeof a&&a.match(/.+\[\{"messages/)?a=JSON.parse(a.replace(/.+\[\{"messages/,'[{"messages').replace("</pre>","")):"string"==typeof a&&(a=JSON.parse(c(a).text()));return a instanceof Array?a[0].messages:a.errors?a.errors:a.success}function g(a){this.msgdiv=c("#"+a.msgdiv),this.prependMsg=a.prependMsg}function h(b,d,e,h,j,k){var l={overlay:!0,showSuccessMsg:!0,prependMsg:!1,form:c("form").first(),scrollToError:!1,errorsOnField:!0};void 0!==d?(l.result=b,l.form=d,l.msgdiv=e,l.leave_open=h,l.not_reset_form=j,l.prefix=k):l=c.extend({},l,b);var m=new g(l);l.overlay&&c("#overlay").css("display","none"),m.msgdiv.find("p.error").remove();var n,o="";if(n=l.form instanceof c?l.form:c("#"+l.form),c.each(f(l.result),function(b,d){for(var e in d){var f=d[e];if("success"===e)f instanceof Array?c.each(f,function(a,b){o+=c("<textarea />").html('<p class="success">'+b+"</p>").text()}):o+=c("<textarea />").html('<p class="success">'+f+"</p>").text();else if("su_"===e.substring(0,3))c(c("label[for='"+e.substring(3)+"']")).not(".static_label").each(function(){c(this).addClass("success_label"),c(this).html(i(this,l)+" "+f)}),c(".errors_above").css("display","block"),"captcha"!==e&&"generic"!==e||m.addGenericError(f);else{if("output"===e)return;if(e=e.replace("ä","ä"),e=e.replace("Ä","Ä"),e=e.replace("ö","ö"),e=e.replace("Ö","Ö"),e=e.replace("ü","ü"),e=e.replace("Ü","Ü"),"captcha"===(e=e.replace("ß","ß"))||"generic"===e)m.addGenericError(f);else{var g=n.find("label[for='"+e+"']").not(".static_label");if(g.each(function(){c(this).addClass("error_label");var b=i(this,l)+" "+f,d=a.getElementById(this.htmlFor);if(l.errorsOnField&&d)if(void 0===d.setCustomValidity||d.getAttribute("aria-hidden"))c(d).addClass("error");else{d.setCustomValidity(b);var e=function(){d.setCustomValidity(""),d.removeEventListener("change",e)};d.addEventListener("change",e)}c(this).html(b)}),g.length<=0){var h=e.replace(/_/," ");l.capitalize&&(h=h.slice(0,1).toUpperCase()+h.slice(1)),m.addGenericError(h+" "+f)}}c(".errors_above").css("display","block")}}}),o)l.leave_open||n.hide("fast"),l.not_reset_form||n.get(0).reset(),l.showSuccessMsg&&(m.msgdiv.css("display","block"),m.msgdiv.find("p").css("display","block"),m.msgdiv.find("p.error").css("display","none"),m.addGenericMessage(o),m.msgdiv.offset().top<c(a).scrollTop()&&c("html, body").animate({scrollTop:m.msgdiv.offset().top},"fast")),"function"==typeof l.success&&l.success(l.result),"undefined"!=typeof redirect_set&&redirect_set?redirect():"function"==typeof l.redirect&&l.redirect();else{if(l.scrollToError){var p=m.msgdiv.find(".error");0===p.length&&(p=n.find(".error_label, .error")),c("html, body").animate({scrollTop:p.offset().top},"fast")}"function"==typeof l.error&&l.error(l.result)}}function i(a,b){var d=c(a).data("lab")||c(a).attr("lab")||c(a).attr("for");return b.prefix&&(d=d.replace(b.prefix,"")),b.capitalize&&(d=d.slice(0,1).toUpperCase()+d.slice(1)),d=d.replace(/_/g," "),c(a).data("lab")||c(a).data("lab",d),d}function j(b,c,d,e){var f=a.getElementById(d).value,g=f.length;return a.getElementById("counter"+b).value=e?c-g:g,g>c?(a.getElementById("counter"+b).style.background="pink",alert("Please keep your answers within "+c+" characters. And please, no pasting of any word processing files. Thank you."),!1):void(a.getElementById("counter"+b).style.background="#fff")}function k(a){namount=Math.abs(a),namount=parseInt(100*(namount+.005)),namount/=100;var b=String(namount);return b.indexOf(".")<0&&(b+=".00"),b.indexOf(".")===b.length-2&&(b+="0"),b}g.prototype.addGenericMessage=function(a){this.msgdiv.css("display","block"),this.prependMsg?this.msgdiv.prepend(a):this.msgdiv.append(a)},g.prototype.addGenericError=function(a){this.addGenericMessage('<p class="error">'+a+"</p>")},g.prototype.reportServerErrorCallback=function(){var a=this;return function(b,c){var d="";switch(c){case"timeout":d="Form submission timed out. Please try again.";break;case"abort":d="Form submission was cancelled.";break;case"error":default:d="An error has occurred. Please try again."}a.addGenericError(d)}},c(function(){c(a).on("keyup","textarea.counter",function(){var a=c(this).val(),b=a.length;return c(this).hasClass("count_down")?c(this).siblings("input.counter").val(c(this).attr("size")-b):c(this).siblings("input.counter").val(b),b>c(this).attr("size")?(c(this).siblings("input.counter").css("background","pink"),alert("Please keep your answers within "+c(this).attr("size")+" characters. And please, no pasting of any word processing files. Thank you."),!1):void c(this).siblings("input.counter").css("background","#fff")})}),b.normalize_labels=e,b.parse_results=h,b.counter=j,b.format_currency=k,b.FormFunctions=g}(document,window,jQuery);