Skip to content

Instantly share code, notes, and snippets.

@arxpoetica
Created November 7, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arxpoetica/864719a797179f42129332e1e6c45b3b to your computer and use it in GitHub Desktop.
Save arxpoetica/864719a797179f42129332e1e6c45b3b to your computer and use it in GitHub Desktop.
Unfortunate var `happy` goes bye bye
// happy forms: http://happyjs.com/
!function(a){function b(b){return"".trim?b.val().trim():a.trim(b.val())}a.fn.isHappy=function(c){function g(a){return!!(a&&a.constructor&&a.call&&a.apply)}function h(b){var d=c.classes&&c.classes.message||"unhappyMessage";return a('<span id="'+b.id+'" class="'+d+'" role="alert">'+b.message+"</span>")}function i(a){return g(c.errorTemplate)?c.errorTemplate(a):h(a)}function j(){var a,b,e=!1;for(a=0,b=d.length;b>a;a+=1)d[a].testValid(!0)||(e=!0);return e?(g(c.unHappy)&&c.unHappy(),!1):c.testMode?(g(c.happy)&&c.happy(),window.console&&console.warn("would have submitted"),!1):(g(c.happy)&&c.happy(),void 0)}function k(){f=!1;}function l(){f=!0,a(window).bind("mouseup",k);}function m(e,h){var j=a(h),k={message:e.message||"",id:h.slice(1)+"_unhappy"},l=a(k.id).length>0?a(k.id):i(k),m=function(){f?a(window).bind("mouseup",j.testValid.bind(this)):j.testValid();};d.push(j),j.testValid=function(d){var f,h,i,k=a(this),m=e.errorTarget&&a(e.errorTarget)||k,n=!1,o=!!k.get(0).attributes.getNamedItem("required")||e.required,p="password"===j.attr("type"),q=g(e.arg)?e.arg():e.arg,r=c.classes&&c.classes.field||"unhappy";return k.length>1?(f=[],k.each(function(b,c){f.push(a(c).val());}),f=f.join(",")):(f=g(e.clean)?e.clean(k.val()):!p&&"undefined"==typeof e.trim||e.trim?b(k):k.val(),k.val(f)),h=(f.length>0||"sometimes"===o)&&g(e.test),d===!0&&o===!0&&0===f.length?n=!0:h&&(n=!e.test(f,q)),n?(m.addClass(r).after(l),!1):(i=l.get(0),i.parentNode&&i.parentNode.removeChild(i),m.removeClass(r),!0)},j.bind(e.when||c.when||"blur",m);}var e,d=[],f=!1;for(e in c.fields)m(c.fields[e],e);return a(c.submitButton||this).bind("mousedown",l),c.submitButton?a(c.submitButton).click(j):this.bind("submit",j),this};}(window.jQuery||window.Zepto);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment