How to use maxlength on textarea in struts 1 html tag
Asked Answered
L

4

5

In struts 1 tags for html:textarea here

I do not see a maxlength attribute. How can i restrict the user from entering more than 100 characters? Also why did struts 1 omit such a basic attribute?

Lowrey answered 19/6, 2013 at 1:26 Comment(0)
F
3

Because this "basic attribute" wasn't even supported in IE before IE10, and was added in HTML 5.

Struts 1 predates that by a significant margin, has been EOL'd, and hasn't seen a new release for some time. Bear in mind that Struts 1.2's last release was, what, 2006-ish?

You may extract the TLD and modify it to allow arbitrary attributes under JSP 2.0:

<dynamic-attributes>true</dynamic-attributes>
Flirtatious answered 19/6, 2013 at 1:59 Comment(0)
V
6

Add the attribute using JS.

Put something like this after your textarea:

<script type="text/javascript">
    document.getElementById('textarea-id').setAttribute('maxlength', '512');
</script>
Vet answered 7/11, 2013 at 12:11 Comment(0)
F
3

Because this "basic attribute" wasn't even supported in IE before IE10, and was added in HTML 5.

Struts 1 predates that by a significant margin, has been EOL'd, and hasn't seen a new release for some time. Bear in mind that Struts 1.2's last release was, what, 2006-ish?

You may extract the TLD and modify it to allow arbitrary attributes under JSP 2.0:

<dynamic-attributes>true</dynamic-attributes>
Flirtatious answered 19/6, 2013 at 1:59 Comment(0)
P
2
  1. add styleId attribute to the html:textarea, struts will not accept id attribute.
  2. add Javascript to limit the length after that

    <html:textarea styleId="contactText" property="contactText" cols="55" rows="10"/>
    
    <script type="text/javascript">
        document.getElementById('contactText').setAttribute('maxlength', '10');
    </script>
    
Pragmaticism answered 23/4, 2015 at 20:28 Comment(0)
B
-2

You may use maxlength="100" within the text area or input in JSP, or else you have to customize validator.xml.

In validator.xml:

<field property="propertyname" depends="maxlength">
              <var-name>maxlength</var-name>
                    <var-value>100</var-value>
                  </var>
                  <msg key="propertyname.maxlength" name="maxlength" />
</field>

In Application.proprties:

propertyname.maxlength = <tr><td colspan\="2" class\="note">Please make sure that FieldName is only  maximum 100 characters.</td></tr>
Burmeister answered 25/6, 2013 at 9:43 Comment(1)
A "maxlength" property on a div is meaningless; divs don't have length. Validation happens either on the server or client side, but doesn't stop the user from typing additional characters w/o JavaScript.Flirtatious

© 2022 - 2024 — McMap. All rights reserved.