![]() |
Sentence case
Need a script that converts the text (lower or upper case) to sentence caps on onkeyup. That is it gets formatted/converted while typing.
|
Some quick thoughts are:-
1. Firstly, why not do it onkeydown, because a character is generally drawn after onkeydown, not onkeyup (at least for IE). 2. There may be a simple answer of capturing the "onkeydown" keycode on the event and overriding it with the desired value. I am not sure if this would work, but is an idea. See:- http://www.msdn.microsoft.com/worksh...es/keycode.asp // IE ONLY FUNCTION document.onkeydown=function() { var nKey=window.event.keyCode; var nNewKey=(String.fromCharCode(nKey).toUpperCase()).charCodeAt(0); window.event.keyCode=nNewKey; } 3. A more messy method could be to run a "captialise function" on the whole inputbox/textarea contents on each key up (but this may lose the cursor). 4. The most sophisticated method would be based on the range and selection objects. There are two different range objects depending on whether you use IE or Mozilla. // IE Range http://www.msdn.microsoft.com/worksh...asp?frame=true //Mozilla DOM Range http://www.mozilla.org/docs/dom/domr...range_ref.html http://www.mozilla.org/docs/dom/domr...dow_ref24.html When a keydown event occurs do something like the following (IE only, not tested):- // IE ONLY FUNCTION document.onkeydown=function() { var nKey=window.event.keyCode; var sNewKey=String.fromCharCode(nKey).toUpperCase(); var oSel=window.selection; var oRange=Sel.createRange(); oRange.collapse(true); // Move to start of range oRange.text=sNewKey; oRange.collapse(false); // Move to end of range oRange.select(); window.event.returnValue=false; // Stop original key press } Hope this gives you some way to go. |
Sorry, I have just noted that you were looking at "Sentence caps". I probably have not answered your question.
|
Hi!
I don't know the difference between sentence key and sentence caps (in my language, both wouldn't make sense anyway). This is sentence KEY: PHP Code:
|
Some additional thoughts are:-
1. Sentence case presumably means that you will need to look at the "context" of any key press. I.e. you cannot simply tell from the keyCode whether it should be upper or lower case, but will need to look back at what has been typed so far. This could be a complex thing to do at the time of an onkeyup event. You may wish to apply it later. 2. If you are going to do it, then my suggestion number 3. in the first post will be perhaps the way to do it. I.e. apply a SentenceCase function to the whole contents of the text box (or at least the contents up to the cursor) after each Key press 3. A Sentence case function may be a non trivial function. I.e. the computer will need to determine whether the character should be capital or not depending on whether it is the start of a sentence. This could be non-trivial if the range of subjects being typed could include names (which need a capital first letter), web addresses (which use lots of dots), etc. You may wish to search on Google for examples of such functions. For instance see:- http://www.search-network.com/web_dev_article9.htm |
Sorry Pittimann, my typo. I mean't Sentence case.
You have anticipated my reply. |
Hi baconbutty.
It was not your typo. :D GurusGuru had 'Sentence case' in his/her subject and 'sentence caps' in the post itself. And if the latter really means something: I don't know it. Cheers - Pit |
Sorry about case and caps. This is what happens if one has limited knowledge. Thanks for the script. Got more than what I expected. No simultaneous dots. So one cannot type etc.....
Just one more thing. How do I use the script for 2 or more text boxes on the same page? |
Hi!
You're welcome! :) Quote:
Regards - Pit |
Hi!
In reply to your edit of the latest post you sent: PHP Code:
|
Thanks again.
|
Hi!
No prob. ;) Cheers - Pit |
What modifications need to be made to the above script so that it can accept 3 letters in caps. Eg. USA. If one types 4 or more letters in caps consecutively then it should make it in lower case. Eg. COUNTRY should become country and not COUntry.
|
| All times are GMT -5. The time now is 01:14 PM. |
Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.