Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:28:21+00:00 2026-06-03T01:28:21+00:00

i am using a function that limit textArea length. Although function is performing well

  • 0

i am using a function that limit textArea length. Although function is performing well but i want to ask whether i am calling function in right way?

Here it is

<h:inputTextarea id="questionInputTextArea"
                 value="#{faqAddUpdate.question}"
                 cols="50"
                 rows="3"
                 disabled="#{faqAddUpdate.buttonState}"
                 onkeypress="limitTextArea(this, 400)"
                 style="overflow: auto;">

   <f:validateLength maximum="200" />

</h:inputTextarea>

Here is the function

function limitTextArea(element, limit) {

    var $textArea = $(element);     //casting to jQuery so we can use jQuery functions on it
    ($textArea).keypress(function(event){

        if (event.which < 0x20) {

            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return;     // Do nothing

        }

        if ($textArea.val().length == limit) {
            event.preventDefault();
        } else if ($textArea.val().length > limit) {

            // Maximum exceeded
            $textArea.val() = $textArea.val().substr(0, limit);
        }

    }); //end of keypress

} //end of function()

The thing that confusing me is this, i am using onkeypress in my jsf code, and in the function i am also saying that ($textArea).keypress(function(event). How can i optimize this function? Or i am doing right?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:28:23+00:00Added an answer on June 3, 2026 at 1:28 am

    You’re attaching a new keypress event listener everytime a key is pressed. This makes no sense. You need to either specify it straight in onkeypress attribute, or to attach it by jQuery means after document ready. Further the assignment to val() function makes no sense, you’d need to pass the new value as its argument instead. This can impossibly be “performing well”. Finally, I’d also suggest to use keyup instead of keypress so that the function is only invoked after the value of the textarea is been updated which results in a more sane behaviour of the limit function. This way you also don’t need to check for non-printable character ranges.

    All with all, it can just look like this:

    function limitTextArea(element, limit) {
        var $textArea = $(element);
    
        if ($textArea.val().length > limit) {
            $textArea.val($textArea.val().substr(0, limit));
        }
    } 
    

    which is to be registered as

    <h:inputTextarea ... onkeyup="limitTextArea(this, 400)" />
    

    If you really need to attach it by jQuery keyup() function, then you’d have to specify the limit in the function yourself.

    E.g.

    <h:inputTextarea ... styleClass="limit" />
    

    with

    $("textarea.limit").keyup(function() {
        var $textarea = $(this);
        var limit = 400;
    
        if ($textarea.val().length > limit) {
            $textarea.val($textarea.val().substr(0, limit));
        }
    });
    

    (which you execute during $(document).ready(function() {...}) or $(function() {...});)


    Or you could specify the limit as part of class name and extract it accordingly:

    <h:inputTextarea ... styleClass="limit_400" />
    

    with something like this

    $("textarea[class*=limit_]").each(function(i, textarea) {
        var classNames = textarea.className.split(' ');
        var limit = 0;
    
        for (var i = 0; i < classNames.length; i++) {
            if (classNames[i].indexOf("limit_") == 0) {
                limit = parseInt(classNames[i].split("_")[1]);
                break;
            }
        }
    
        if (limit) {
            $(textarea).keyup(function() {
                var $textarea = $(this);
    
                if ($textarea.val().length > limit) {
                    $textarea.val($textarea.val().substr(0, limit));
                }
            });
        }
    });
    

    By the way, the limit of 400 doesn’t match the limit which you’ve there in the server side <f:validateLength> validator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The jquery function that I'm using is: $.getJSON(rpc2.php?queryString= +inputString+, function(data) { if(data.length >0) {
i am using inputTextArea. Actually i want to limit it's max length. I am
I'm using a function that uploads an image, takes the stream and resizes it
Here is an (artificial) example of using a function that returns an anonymous struct
I'm using the following function that changes a calendar selection at a set time
I am using this jquery function that works fine in win/mac FF 3.5 and
ive a function that does calculations on a table using jquery when any input
I have a function that I'm using while(true) to repeatedly scan memory addresses of
I have the following function that I am using to remove the characters \04
I have a PHP function that takes a variable number of arguments (using func_num_args()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.