1

When creating a html form using the textarea attribute. I have stumbled into a problem. I have restrained the textarea to a minimum of 30. However When i submit the form without putting anything in the textarea, the form gets processed. In contrast, when i enter lets say 1 character and then submit the form, the proper error message shows up, indicating to the user that the text does not meet the minimum requirements.

The question is, how do i make it so when i don't enter anything in the textarea, and submit the form, i get shown an error message rather than the form proceeding with the submission process.

 <TEXTAREA name="text" ROWS="6" COLS="25" maxlength="500" minlength="30"> </TEXTAREA> 
HashTables
  • 392
  • 2
  • 7
  • 22
  • I think there is no minlength property exists for TextArea both in HTML 4.1 and HTML 5. Instead check this post http://stackoverflow.com/questions/18184791/how-to-apply-min-and-max-on-textarea – TheKingPinMirza May 23 '15 at 10:36
  • Possible duplicate of [Is there a minlength validation attribute in HTML5?](https://stackoverflow.com/questions/10281962/is-there-a-minlength-validation-attribute-in-html5) – Samuli Hakoniemi Oct 21 '17 at 10:07

3 Answers3

1

According to http://www.w3schools.com/tags/tag_textarea.asp, textarea elements do not support minlength.

You need extra validation.

See this post: Is there a minlength validation attribute in HTML5?

Try this jQuery validation

$( "form" ).submit(function( event ) {
    if ( $( "#textareaId" ).val().length >0 ) {
        // ok
        return;
    }

    alert("not valid");
    event.preventDefault();
});
Community
  • 1
  • 1
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
  • Hi, the minlength attribute works fine for me. Just only when i don't put anything in the textfield, the minlength constraint ceases to work. – HashTables May 23 '15 at 10:27
1

Adding required="required" as an attribute to the textarea should solve the problem for you.

Here are more details from www.w3.org:

The minlength attribute does not imply the required attribute. If the form control has no minlength attribute, then the value can still be omitted; the minlength attribute only kicks in once the user has entered a value at all. If the empty string is not allowed, then the required attribute also needs to be set.

A note of caution

One thing to keep in mind, however, is that there is no official minlength attribute for textareas. Browser support is also still quite limited for minlength for input fields. IE and Edge do not support it. Many other browsers only added support for it in 2017. And that is for input boxes, not textareas. The workaround for input fields is to use a regular expression such as:

<input pattern=".{30,500}">

But, there is no pattern attribute for textboxes either.

So, if you want something that works across most browsers, using a JavaScript option would be your best bet.

kojow7
  • 10,308
  • 17
  • 80
  • 135
-2

You have

minlength=30

It should be

minlength="30"
mrfloden
  • 74
  • 7
  • 1
    Quotes around HTML attribute values are not actually required. You should still use them but they aren't necessarily required. – Wesley Murch May 30 '15 at 06:21