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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:43:31+00:00 2026-05-16T04:43:31+00:00

I’ve written a jQuery function to check the total number of characters in a

  • 0

I’ve written a jQuery function to check the total number of characters in a given textbox and pop up a warning if it exceeds the maximum (user-defined) length.

Example: (greatly simplified):

function checkLength(txt)
{
   if (txt.length >= maxlength) 
   {
      alert('Too much text entered');
      return false;
   }
   else
      return true;
}

This works as expected, but like a good little developer I also perform server-side validation against the length of the field the entered text is assigned to (on my domain object).

It’s there that I noticed that the following string:

12<RETURN>34 

(where <RETURN> is obviously the return character)

will return 5 as length in Javascript
and return 6 as length in .NET (String.Length)

Am I missing something here? Or will I be forced to count the number of returns in the javascript string and then double them to get the ‘correct’ .NET count? Any pointers on how to handle this?

UPDATE: I can indeed get the correct length by doing the replace as suggested below.

function GetCurrentLength(txt) {
   return txt.replace(/\n/g, "\n\r").length;
}

However, there is more to the question than detecting the character count, because once the system detects that too much characters have been entered, it should (client-side) strip all excess characters away (this can be more than one, because the users might copy-paste too much text into the textbox).

Something like:

if (textLength > maxlength) {
    self.val(text.substr(0, maxlength));
}

This of course will get me into trouble if I count returns double, because the wrong number of characters risk being deleted. How can I reliably delete the right number of characters then?

  • 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-05-16T04:43:32+00:00Added an answer on May 16, 2026 at 4:43 am

    This was my final solution, feels a bit dirty, but does the job.

    // Calculate the length of a string, but take care to count returns for 2 characters instead 
            // of one. That way, we should get the same length as in .NET
    
            function GetCurrentLength(txt) {
                return txt.length + GetNumberOfReturns(txt);
            }
    
            function GetNumberOfReturns(txt)
            {
                var numberOfReturns = 0
                var enterRegExpr = txt.match(/\n/g);
                if (enterRegExpr)
                    numberOfReturns = enterRegExpr.length ;
    
                return numberOfReturns;
            }
    
            function CutToMaxLength(txt, maxlength) {
                return txt.substr(0, maxlength - GetNumberOfReturns(txt));
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I

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.