First, thanks for the editor!
I have text fields that are limited to a certain number of characters, let's say 200.
Now I know how to add a counter to a text field, but I was wondering how this is possible when your textarea has been replace by CKeditor.
Also, when you write "CKeditor" in a textfield you have 8 characters; if you do the same in a CKeditor instance you have 15 characters, because the entry is really "<p>CKeditor</p>".
Does anybody have any solutions for this?
I have put up a test page at http://demo.goldroo.com/postnews/ which has 2 the same textareas, except that one is replaced by a CKeditor instance.
Cheers
Fri, 09/11/2009 - 04:07
#1
Re: Character count
This code should help you to get character count (without whitespaces).
Although it's not maxlength functionality, which is covered by ticket #397.
Re: Character count
Thanks Tobias(?)
Now I am not sure with that code, though, I am not that smart
Any more pointers about how I could that work for the example I put up?
I actually would like to count all characters, including spaces, <, > and /.
I'd be quite happy to leave the sample page up and add some explanations to it, since this might help some others.
Cheers
Re: Character count
Re: Character count
Sorry for asking such a stupid question, but where do you put this code ? I know so little about Javascript that I'm always lost.
My request is both for a character counter (not including HTML tags, if possible) AND a maxlength functionality.
I know this plugin http://sourceforge.net/tracker/index.php?func=detail&aid=1510662&group_id=75348&atid=737639 but I don't know how to make it work.
I need different maximum lengths for 3 textareas in the same form.
I would really appreciate some help...
Maybe I should ask this question on the support forum ?
Re: Character count
Thanks Gary, that works like you can see at http://demo.goldroo.com/postnews/.
It seems to be a little bit erratic, as in maybe a few characters out sometimes, but it will do for what I need it for.
To Gytha_Ogg: have a look at the code/source of http://demo.goldroo.com/postnews/.
Hopefully that gives you some idea where to put the code.
Thanks again to Tobias and Gary.
Re: Character count
Thanks a lot, Poirot. Yesterday, I had a look at your source code and it didn't work. Today, it does work !
This code works properly, anyway, I would prefer to prevent people from typing more characters than the number allowed. Is it possible ?
Re: Character count
While it's not an easy-to-achieve functionality, a solution could be found based on the editor's undo/redo system, I hope the following codes could be a rough idea for you while it's not functional complete( require modification when working with IME):
Re: Character count
I thank you, Garry. I'm going to test this.
Re: Character count
I have added Gary's latest code to http://demo.goldroo.com/postnews/
and of course it works again.
It is interesting to see that when you copy all the HTML from the source of the CKeditor instance and paste it in the raw HTML textarea, you can get a few characters less or more.
Not sure what you mean with "require modification when working with IME".
I tested in IE8, FF, Safari for Windows and Chrome and it works in all of them.
Re: Character count
There is only a little problem with Opera : when you reach the limit, the last letters of the text are replaced by what you are trying to type. I like Opera but I have to admit that it has always been quite touchy about javascript.
Re: Character count
Re: Character count
For me, Javascript is something like an horrible punishment, and I am grateful to people who can help me in these torments.
Your (or Garry's) code works, and I tried to use it here :
http://www.lgv.on-web.fr/valinfotest/testcke.php
on the first textarea. I have been searching for hours and hours, I don't understand why my example doesn't work, since it is very similar to yours. The counter stops at 100 characters (my limit), but I can go on typing as many characters as I want !. The problem is the same in Firefox or Opera. I don't see what's wrong in my code !
Re: Character count
I didn't made any performance test for the moment.
I follow those steps :
1. remove html tags
2. replace multiple spaces by only one
3. consider HTML entities (é for example) as one character (artificially replaced by the character 'X')
4. remove white spaces at the beginning and end of the text.
Then I begin listening on events. Here is when I get into trouble. I'm currently listening on the event 'key' but it seems that the data returned by 'editor.getData()' is the data before modification.
Is there an event corresponding to 'after content modification' ?
Re: Character count
Re: Character count
The Counter doesn't work on paste a text from (e.g) MS Word. If there any option to add a event handler to do this, please let me know.
Thank you!
problem on paste
I have same problem.
I used this solution but problem is that still not working on paste frome another source.
Is there any good solution for this?
I can see that this topic is old so it should be.
Thank you.