{"id":1199,"date":"2013-03-10T02:59:18","date_gmt":"2013-03-10T08:59:18","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1199"},"modified":"2013-05-07T23:05:38","modified_gmt":"2013-05-08T05:05:38","slug":"code-of-the-day-coffeescript-jquery-enforce-max-length-for-all-input-elements","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2013\/03\/code-of-the-day-coffeescript-jquery-enforce-max-length-for-all-input-elements\/","title":{"rendered":"Code of the Day: Coffeescript + jQuery, enforce max length for all input elements"},"content":{"rendered":"<p>Enforce max length in all browsers since some browsers, IE8, don&#8217;t support maxlength for all input elements.<\/p>\n<p><b>Coffeescript<\/b><\/p>\n<pre class=\"prettyprint linenums lang-javascript\">\r\n# requires jQuery 1.6+ \r\nenforceMaxLength = ->\r\n  $(\"[maxlength]\").on \"blur\", ->\r\n    $(@).val (index,val) -> \r\n      val.substring 0, $(@).attr(\"maxlength\")\r\n\r\nenforceMaxLength()\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Javascript<\/b><br \/>\n<code><\/p>\n<pre lang=\"javascript\">\r\n\/\/ Generated by CoffeeScript 1.6.1\r\n(function() {\r\n  var enforceMaxLength;\r\n\r\n  enforceMaxLength = function() {\r\n    return $(\"[maxlength]\").on(\"blur\", function() {\r\n      return $(this).val(function(index, val) {\r\n        return val.substring(0, $(this).attr(\"maxlength\"));\r\n      });\r\n    });\r\n  };\r\n\r\n  enforceMaxLength();\r\n\r\n}).call(this);\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Enforce max length in all browsers since some browsers, IE8, don&#8217;t support maxlength for all input elements. Coffeescript # requires jQuery 1.6+ enforceMaxLength = -> $(&#8220;[maxlength]&#8221;).on &#8220;blur&#8221;, -> $(@).val (index,val) -> val.substring 0, $(@).attr(&#8220;maxlength&#8221;) enforceMaxLength() Javascript \/\/ Generated by CoffeeScript 1.6.1 (function() { var enforceMaxLength; enforceMaxLength = function() { return $(&#8220;[maxlength]&#8221;).on(&#8220;blur&#8221;, function() { return $(this).val(function(index, &hellip; <a href=\"https:\/\/bateru.com\/news\/2013\/03\/code-of-the-day-coffeescript-jquery-enforce-max-length-for-all-input-elements\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Coffeescript + jQuery, enforce max length for all input elements<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30],"class_list":["post-1199","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1199"}],"version-history":[{"count":3,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1199\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}