{"id":2381,"date":"2020-01-03T16:54:54","date_gmt":"2020-01-03T15:54:54","guid":{"rendered":"https:\/\/da-software.de\/?p=2381"},"modified":"2021-06-25T18:05:47","modified_gmt":"2021-06-25T16:05:47","slug":"limit-the-number-of-characters-in-the-text-field-of-an-html-form","status":"publish","type":"post","link":"https:\/\/da-software.net\/en\/2020\/01\/limit-the-number-of-characters-in-the-text-field-of-an-html-form\/","title":{"rendered":"Limit the number of characters in the text field of an HTML form"},"content":{"rendered":"<p>When designing or publishing an online form, it often happens that you want to limit the number of characters that can be entered in a text field, such as <em><strong>textarea<\/strong> <\/em>or input <em><strong>type=&#8221;text&#8221;<\/strong><\/em>, and you may also want to visualize this to the user, similar to the image below.<\/p>\n<figure id=\"attachment_2075\" aria-describedby=\"caption-attachment-2075\" style=\"width: 764px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2075 size-full\" title=\"Textarea in HTML form with character limitation\" src=\"\/wp-content\/uploads\/2019\/10\/daform_v49_chars_anim.gif\" alt=\"Gif animation of a textarea in online form with character delimitation\" width=\"764\" height=\"408\" \/><figcaption id=\"caption-attachment-2075\" class=\"wp-caption-text\">Example of a text area in online form with character delimitation<\/figcaption><\/figure>\n<p>Especially with free multi-line text fields, as often used in <a title=\"How to create a contact form\" href=\"https:\/\/da-software.de\/en\/2019\/10\/contact-form\/\">contact forms<\/a>, it makes sense to limit the input so that whole novels cannot be copied in or if only a limited number of characters is available for further processing.<br \/>\n<a title=\"only some clicks to limit number of characters for text field in online forms\" href=\"https:\/\/da-software.net\/en\/software\/da-formmaker\/\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/01\/limited-characters-form-field-form-builder.jpg\" alt=\"form generator to build a text field with limited letters\" width=\"269\" height=\"250\" class=\"aligncenter size-full wp-image-5753\" \/><\/a><\/p>\n<h2>Limit characters at &lt;textarea&gt;<\/h2>\n<p>The simple character delimitation in an HTML form for a multiline text field element, i.e. of the type <em><strong>&lt;textarea&gt;<\/strong><\/em> is relatively simple. The HTML syntax provides the attribute <em><strong>maxlenght<\/strong> <\/em>for this purpose. The following sample code shows what a character limit of 50 characters would look like.<\/p>\n<pre>&lt;textarea <strong>maxlength=\"<span style=\"color: #ff9900;\">50<\/span>\"<\/strong>&gt;\r\nText hier eingeben...\r\n&lt;\/textarea&gt; \r\n<\/pre>\n<h2>Limit characters at &lt;input type=&#8221;text&#8221;&gt;.<\/h2>\n<p>With the single-line input field, which is represented in HTML by <em>&lt;input type=&#8221;text&#8221;&gt;<\/em>, the character limit can be inserted in the same way via the <em>maxlength<\/em> attribute as with the multiline text field. The corresponding example code in HTML looks like this. This time with a limit of 100 characters.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">input<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text<\/a>\" <span class=\"attribute-name\">name<\/span>=\"<a class=\"attribute-value\">occasion<\/a>\" <span class=\"attribute-name\">value<\/span>=\"\" <strong><span class=\"attribute-name\">maxLength<\/span>=\"<a class=\"attribute-value\">100<\/a>\"<\/strong>&gt;<\/pre>\n<h2>Limit character input with JavaScript<\/h2>\n<p>Limiting the number of characters in a text field of an HTML form alone leads to a poor user experience or unsightly usability. One simply wonders why it doesn&#8217;t continue despite a strong impact on the keyboard. So you should at least note that only &#8220;<em>XX characters<\/em>&#8221; are available. It&#8217;s even nicer if you can visualize how many characters are left as shown in the gif image above.<\/p>\n<p>For the representation of the character limit one uses an appropriate <a title=\"A javascript course for beginners\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/JavaScript\/First_steps\/What_is_JavaScript\">JavaScript<\/a>. The example refers to a 5-line text field in an online form under which the number of characters is displayed as &#8220;<em>from\/max<\/em>&#8220;.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">div<\/span>&gt;\r\n&lt;<span class=\"start-tag\">textarea<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">comment<\/a>\" <span class=\"attribute-name\">name<\/span>=\"<a class=\"attribute-value\">comment<\/a>\" <span class=\"attribute-name\">rows<\/span>=\"<a class=\"attribute-value\">5<\/a>\" <span class=\"attribute-name\">maxlength<\/span>=\"<a class=\"attribute-value\">500<\/a>\"&gt;&lt;\/<span class=\"end-tag\">textarea<\/span>&gt;\r\n&lt;<span class=\"start-tag\">div<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">the-count_comment<\/a>\" <span class=\"attribute-name\">style<\/span>=\"\"&gt;\r\n&lt;<span class=\"start-tag\">span<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">current_comment<\/a>\"&gt;0&lt;\/<span class=\"end-tag\">span<\/span>&gt;\r\n<span id=\"line673\"><\/span>&lt;<span class=\"start-tag\">span<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">maximum_comment<\/a>\"&gt; \/ 500&lt;\/<span class=\"end-tag\">span<\/span>&gt;\r\n&lt;\/<span class=\"end-tag\">div<\/span>&gt;<\/pre>\n<p>The associated JavaScript can then look like the following. Based on a limit value, the formatting of counted letters is changed in another color to visualize the limited number of characters.<\/p>\n<pre>&lt;script&gt;\r\n$('#comment').keyup(function () {\r\n  var characterCount = $(this).val().length,\r\n  current = $('#current_comment'),\r\n  maximum = $('#maximum_comment'),\r\n  theCount = $('#the-count_comment');\r\n  var maxlength = $(this).attr('maxlength');\r\n  var changeColor = 0.75 * maxlength;\r\n  current.text(characterCount);\r\n\r\n  if (characterCount &gt; changeColor &amp;&amp; characterCount &lt; maxlength) {\r\n    current.css('color', '#FF4500');\r\n    current.css('fontWeight', 'bold');\r\n  }\r\n  else if (characterCount &gt;= maxlength) {\r\n    current.css('color', '#B22222');\r\n    current.css('fontWeight', 'bold');\r\n  }\r\n  else {\r\n    var col = maximum.css('color');\r\n    var fontW = maximum.css('fontWeight');\r\n    current.css('color', col);\r\n    current.css('fontWeight', fontW);\r\n  }\r\n});\r\n&lt;\/script&gt;\r\n<\/pre>\n<p><a title=\"create online form with online form builder\" href=\"https:\/\/da-software.net\/en\/software\/da-formmaker\/\">Form generators<\/a>, like the DA-FormMaker, bring such functions with them. Then you save yourself this work, especially if you have to create several forms, as it is a bit of fiddling until you have configured everything according to your wishes.<\/p>","protected":false},"excerpt":{"rendered":"<p>When designing or publishing an online form, it often happens that you want to limit the number of characters that can be entered in a text field, such as textarea or input type=&#8221;text&#8221;, and you may want to visualize this to the user.<\/p>\n","protected":false},"author":2,"featured_media":2382,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[20],"tags":[92,90,113,162,163,161,111,164,125,165],"class_list":["post-2381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks","tag-da-formmaker-en","tag-form","tag-html-en","tag-html-form","tag-input-en","tag-instruction","tag-internet-form","tag-javascript-en","tag-online-form","tag-textarea-en"],"_links":{"self":[{"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/posts\/2381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/comments?post=2381"}],"version-history":[{"count":0,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/posts\/2381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/media\/2382"}],"wp:attachment":[{"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/media?parent=2381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/categories?post=2381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/da-software.net\/en\/wp-json\/wp\/v2\/tags?post=2381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}