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

  • SEARCH
  • Home
  • 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 7793623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:32:46+00:00 2026-06-01T22:32:46+00:00

i am using the below plugin for counting the char in the textarea but

  • 0

i am using the below plugin for counting the char in the textarea but the only feature i would like to add is that when the character is exceed the limit then it should stop and not allow any more characters to be entered.. so should i just return when it match the allowed character?

below is the sample link with source code.

http://cssglobe.com/lab/charcount/01.html

(function($) {

    $.fn.charCount = function(options){

        // default configuration properties
        var defaults = {    
            allowed: 140,       
            warning: 25,
            css: 'counter',
            counterElement: 'span',
            cssWarning: 'warning',
            cssExceeded: 'exceeded',
            counterText: ''
        }; 

        var options = $.extend(defaults, options); 

        function calculate(obj){
            var count = $(obj).val().length;
            var available = options.allowed - count;
            if(available <= options.warning && available >= 0){
                $(obj).next().addClass(options.cssWarning);
            } else {
                $(obj).next().removeClass(options.cssWarning);
            }
            if(available < 0){
                $(obj).next().addClass(options.cssExceeded);
            } else {
                $(obj).next().removeClass(options.cssExceeded);
            }
            $(obj).next().html(options.counterText + available);
        };

        this.each(function() {              
            $(this).after('<'+ options.counterElement +' class="' + options.css + '">'+ options.counterText +'</'+ options.counterElement +'>');
            calculate(this);
            $(this).keyup(function(){calculate(this)});
            $(this).change(function(){calculate(this)});
        });

    };

})(jQuery);
  • 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-01T22:32:48+00:00Added an answer on June 1, 2026 at 10:32 pm

    A couple small changes for this. First, monitor keydown instead of keyup event. This allows you to preventDefault and keep the new text from being added. Next, you have to allow delete and backspace so that they can correct their mistake. There are a lot of corner cases that you should probably account for like cut/paste, arrow keys, etc. That’s why I don’t like modifying the default behavior for the text area on something like this. You should always do pretransmit and server side double checking anyways so I don’t think it gets you a lot other than the potential to annoy your users.

    http://jsfiddle.net/qfzkw/2/

    (function($) {
    
        $.fn.charCount = function(options) {
    
            // default configuration properties
            var defaults = {
                allowed: 10,
                warning: 5,
                css: 'counter',
                counterElement: 'span',
                cssWarning: 'warning',
                cssExceeded: 'exceeded',
                counterText: '',
                preventTextEntry: true
            };
    
            var options = $.extend(defaults, options);
    
            function calculate(obj, event) {
    
                var count = $(obj).val().length;
                var available = options.allowed - count;
                if (available <= options.warning && available >= 0) {
                    $(obj).next().addClass(options.cssWarning);
                } else {
                    $(obj).next().removeClass(options.cssWarning);
                }
    
                if (available < 0) {
                    if (options.preventTextEntry && event.which != 46 && event.which != 8) { event.preventDefault() };
                    $(obj).next().addClass(options.cssExceeded);                
                } else {
                    $(obj).next().removeClass(options.cssExceeded);
                }
                $(obj).next().html(options.counterText + available);
            };
    
            this.each(function() {
                $(this).after('<' + options.counterElement + ' class="' + options.css + '">' + options.counterText + '</' + options.counterElement + '>');
                calculate(this);
                $(this).keydown(function(e) {
                    calculate(this, e)
                });
                $(this).change(function(e) {
                    calculate(this, e)
                });
            });
    
        };
    
        $('textarea').charCount();
    
    })(jQuery);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the FB comments plugin on my website and would like to feature
using the below jquery placeholder plugin that creates labels and overlays the field with
I am using below code to add number of images in a single cell
I am using below URL to show line chart but one thing I have
I have a class that I am using below. And I am using this
I am using this chat plugin: http://sandbox2.pseudocoder.com/demo/chat In chat.js, I tried using the below
Below is the plugin code that i have tried to reuse from an old
Using the code sample below, how could I utilize the jquery cookie plugin so
I am using this plugin to create charts: http://www.filamentgroup.com/lab/update_to_jquery_visualize_accessible_charts_with_html5_from_designing_with/ The code works below if
I am using jquery + the hashchange plugin from ben alman. Below is a

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.