Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set maxlength attribute on h:inputTextarea

Tags:

How can I limit the length of <h:inputTextarea>? For <h:inputText> it works fine with maxlength attribute. However, this attribute is unavailable in <h:inputTextarea>.

like image 974
sergionni Avatar asked Feb 07 '11 16:02

sergionni


People also ask

How do I add Maxlength to textarea?

Then use JavaScript to limit user input: $(function() { $("textarea[maxlength]"). bind('input propertychange', function() { var maxLength = $(this). attr('maxlength'); if ($(this).

Is Maxlength an attribute?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.

Does textarea have Maxlength?

The HTML <Textarea>maxlength attribute is used to specify the maximum number of characters enters into the Textarea element. Attribute Value: number: It contains single value number which allows the maximum number of character in Textarea element. Its default value is 524288.

Which attribute is used to limit the text to be given in the textarea?

The maxlength attribute specifies the maximum length (in characters) of a text area.


2 Answers

HTML5 + JSF 2.2+

If you're using HTML5 and JSF 2.2+, specify it as a passthrough attribute.

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">  <h:inputTextarea value="#{bean.text}" a:maxlength="2000" /> 

HTML5 + JSF 2.0/2.1

If you're using HTML5, but not JSF 2.2 yet, use OmniFaces Html5RenderKit to recognize new HTML5 attributes in JSF 2.0/2.1.

<h:inputTextarea value="#{bean.text}" maxlength="2000" /> 

HTML4

If you're on HTML4, then it's already not supported by HTML itself. It's only supported on <input> element, not on <textarea> element. That's also why there's no such attribute on the JSF representation of this HTML element. You need to solve this requirement at the client side using JS and/or at the server side using JSF. JS enables you to instantly validate the length and ignore all other characters. JSF enables you to validate it as well for the case that the client disabled or hacked the JS code. Best would be a combination of both.

Assuming that you've a

<h:inputTextarea value="#{bean.text}" styleClass="max">     <f:validateLength maximum="2000" /> </h:inputTextarea> 

here's how you could do the jQuery

$('textarea.max').keyup(function() {     var $textarea = $(this);     var max = 2000;     if ($textarea.val().length > max) {         $textarea.val($textarea.val().substr(0, max));     } }); 
like image 126
BalusC Avatar answered Sep 21 '22 17:09

BalusC


<h:inputTextarea  required="true" cols="50" rows="5" id=”aboutMe” value="#{person.aboutMe}”>           <f:validateLength maximum="400" minimum="20"/>     </h:inputTextarea> 
like image 22
jmj Avatar answered Sep 23 '22 17:09

jmj