Results 1 to 13 of 13

Thread: Javascript count characters

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Javascript count characters

    Being a server side script man this javascript stuff does my head in.

    I have a multi line text box and must ensure that the usert does not enter more than 5 lines.

    This would include either a long paragraph or lets say, acouple of lines and 3 carriage returns.

    Does anyone have any idea how I would handle this ?

    Thanks in Advance

    Parksie

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    Why is there a limitation, what's it for?
    Would five single line textfields work or would that just be too ugly.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I cant use 5 text boxes.

    The boss demends a multiline textbox.

    I know where your coming from. It would be a lot easier that way but I'm just the monkey, not the grinder.


    Parksie

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    5 lines? That is unrealistic. If you don't only count newlines then the line count depends on the width of the textbox, which causes problems. Let's take vBulletin as an example. The width of the textbox I'm writing in is set via the "cols" property. This property means that there can be a maximum of 70 characters on one line.
    First problem: you can now take the character count of a paragraph and divide it by 70 to get the line count. But there's the problem of wrapping. If my paragraph consists mostly of
    unreadabeLongWords IMightHaveInventedOrConstructedJustToBrakeThe system, then the wrapping can cause huge errors by displaying as little as 18 characters on one line.
    Another problem are variable-spaced fonts. You don't see it in post view, but this paragraph contains 80 whitespace just before the word "Another". Similar effects could be achieved by using long strings of is (shortest) or ms (longest).

    Even more interesting is that setting the width with the cols property is a huge mistake anyway. If you have ever used a vBulletin board with Mozilla (as I do when I'm at home), you'll have noticed that the post textarea is narrow, only about half as wide as in IE.
    No, this is not a bug, it is just the inconsideration of web designers for various browsers Mozilla simply happens to use a different way of computing the width of a character (average width instead of maximal width I think) that results in the required width for 70 characters being less.
    The solution to this is using the width CSS property to set the text box to a font-independent width (as was done at www.galah.net , at my request). Now you have a textarea that has always the same size in all browsers (compliant at least). And you have no way of finding out how many characters can fit on one line. Just two slight movements of two of my fingers can change the general font zoom, and in Mozilla (unlike IE) this affects text boxes just as normal text. And there's quite a difference in the amount of text fitting into a line in 200% zoom and 50% zoom.


    You see, judging the width leads inevitably to unportable and unreliable code. Maybe you should set a character count limit instead, assisted by a newline limit.
    E.g. no more than 300 characters and no more than 3 newlines.

    Talk to your boss, explain the problems, you can even print this post and show it to him. You will need a different solution.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    You see, judging the width leads inevitably to unportable and unreliable code. Maybe you should set a character count limit instead, assisted by a newline limit.

    Thanks CB.

    I think the character count limit with a carriage return limit is the way formward but I have no idea how to do this kind of thing client-side.

    How can I count every character the user enters in the memo field and stop them from entering any more ?

    This is gonna kill me.


    Bang

    Parksie

  6. #6
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    First off, it has been a few weeks since I've done any JavaScript, so I can't remember correct terminology. I've been stuck in VBA land for a little while, so my vocabulary has changed. To that end, I remind everyone that I am still looking for a Real Job (tm). VB/VBA/VBScript sucks.

    On the change event of the text box, check the length. If the length is longer than x, trim the value down to x. If the string contains four newlines, trim the string down by 1 character.

    If you really need the JavaScript code for this, I could put that together tonight. In the meantime, check my profile. There are links to the authoritative JavaScript documentation.

    To go back to CornedBee's post. What he says is true, but it makes me wonder. I can't test this since all I have is IE at work, but... Can we set the font in input to a fixed-width font and set the width of the input to 70 characters (ex?). When the user zooms, will the input change size?

    I'd imagine IE won't. But even in Mozilla, there are some things that the browser leaves up to the window manager. Mozilla may not even attempt to change the size of the input, I don't know. There is a very good reason why you can't change the look/feel of the inputs. I just don't know if the size of the text box would fall in that category.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Just tested Mozilla, it changes the size of the text in the input (at least here).

    Using a monospaced font would certainly resolve one of the issues - but only one.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Thanks for all your help guys.

    They have no idea how difficult something like this is.

    I hate working for graphic designers.

    Parksie

  9. #9

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    You wont believe it but they actually want me to write some vbscript code that parses the text.

    e.g. The user enters as much or as little text into a memo field and when it displays my code ensures that :

    It doesnt display more then x characters.
    It doesnt display more than x lines.
    It always ends at the end of a sentence.
    It never ends a line in the middle of a word.

    It tracks what page your on and displays the usual :

    "next prev and this is page 1 of x pages."

    Can someone please tell me if this is the craziest most moronic thing ever ?

    This is why I thought it would be better to use this method. The user enters data into the memo field with the javascript validation and then inserts a new record and keeps doing this until they have finished. Then all I have to do is display the memo field and have some simple recordset navigation to allow them to go to next, prev record.

    Bar Stewards

    Parksie

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    >It doesnt display more then x characters.
    Easy.

    >It doesnt display more than x lines.
    Impossible, as I explained.

    >It always ends at the end of a sentence.
    Need it be a grammatically valid sentence?
    Just checking to see if it ends with a ./!/? is easy.

    >It never ends a line in the middle of a word.
    What is a word? What if my last word is a name like Günther? Do they expect you to have a dictionary of all words and names in all languages available to JavaScript (i.e. embedded in the HTML code)? Why don't they instead add a 200000x200000 pixel image with 128 bit color depth to the page?
    What if the last word is Egwene? Frodo? I can invent words and it might make sense in context.
    In short: what is the end of a word?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Thumbs down

    a space, but can this be trapped as an ASCII code in javascript

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A space? So this string: "Hello, how are you?" does end in the middle of a word? And this does not: "Hello, how are you? " ???
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    good point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width