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 8012105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:13:37+00:00 2026-06-04T19:13:37+00:00

Below is my code that prevents the user from typing more than 10 characters

  • 0

Below is my code that prevents the user from typing more than 10 characters in an input box, as well as providing the user with information of how many characters that’s left.

The problem is that if the user is erasing what he’s typed (on or more characters), the number of characters isn’t updated.

So, my questions is: How can I detect if the user is erasing in the input box?

Thanks in advance!

    $('.imageTitle').keypress(function(e) {

        var value = $(this).val(),
            valueLength = value.length,
            set = 10,
            remain = parseInt(set - valueLength);

        if (remain <= 0 && e.which !== 0 && e.charCode !== 0) {
            $('.imageTitle').val((value).substring(0, valueLength - 1))
        }

        $('#titleCharsLeft').html('&nbsp' + remain);
    });
  • 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-04T19:13:39+00:00Added an answer on June 4, 2026 at 7:13 pm

    If I am not mistaken You dont need any special mechanism to detect erasing You can do that with simple $.keyup. Use the following code. It updates the remaining character like Twitter but you can modify it to count total characters too.

    $("input[maxlength]").each(function() {
        var $this = $(this);
        var maxLength = parseInt($this.attr('maxlength'));
        $this.attr('maxlength', null);
    
        var el = $("<span class=\"character-count\">" + maxLength + "</span>");
        el.insertAfter($this);
    
        $this.bind('keyup', function() {
            var cc = $this.val().length;
    
            el.text(maxLength - cc);
    
            if(maxLength < cc) {
                el.css('color', 'red');
            } else {
                el.css('color', null);
            }
        });
    });​
    

    Working Fiddle

    It’s a generalized one which should get you started. Modify it to suit your need.

    UPDATE

    You can even add support for input event on browsers which support it for events like paste, long keypress or drag. Like this

    var maxLength = 100;
    var supportOnInput = 'oninput' in document.createElement('input');
    $('.imageTitle').on(supportOnInput ? 'input' : 'keyup',function(){
        var cc = $(this).val().length;
    
            $('#titleCharsLeft').text(maxLength - cc);
    
            if(maxLength < cc) {
                $('#titleCharsLeft').css('color', 'red');
            } else {
                $('#titleCharsLeft').css('color', 'black');
            }
    });
    ​
    

    Modified Fiddle to more suit your need.

    Reference: oninput or this

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

Sidebar

Related Questions

the below code is working fine if the user enters the USN number that
below code is my databasehandler class i got it from a tutorial. Beside that
Below is the code that I'm using in ms access to list the data
I have the code below that hides and shows the navigational bar. It is
I have this javascript code below that uses jquery, it is suppoed to be
Background: I have a WPF UserControl (MainControl - not shown in code below) that
below i have a code that runs in most of my simple programs ..
I understand that the code below would result segmentation fault because at the cstr
I am using the below code, but I am not getting that why it
I've been receiving errors with the following code below saying that the index is

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.