WebDeveloper.com

WebDeveloper.com (http://www.webdeveloper.com/forum/index.php)
-   JavaScript (http://www.webdeveloper.com/forum/forumdisplay.php?f=3)
-   -   Sentence case (http://www.webdeveloper.com/forum/showthread.php?t=41676)

GurusGuru 08-13-2004 01:32 AM

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.

baconbutty 08-13-2004 03:08 AM

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.

baconbutty 08-13-2004 03:10 AM

Sorry, I have just noted that you were looking at "Sentence caps". I probably have not answered your question.

Pittimann 08-13-2004 03:22 AM

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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html>
<
head>
<
title>Untitled</title>
<
script language="JavaScript" type="text/javascript">
<!--
function
sCase(){
val=document.forms[0].convert.value;
result=new Array();
result2='';
count=0;
endSentence=new Array();
for (var
i=1;i<val.length;i++){
if(
val.charAt(i)=='.'||val.charAt(i)=='!'||val.charAt(i)=='?'){
endSentence[count]=val.charAt(i);
count++
}
}
var
val2=val.split(/[.|?|!]/);
if(
val2[val2.length-1]=='')val2.length=val2.length-1;
for (var
j=0;j<val2.length;j++){
val3=val2[j];
if(
val3.substring(0,1)!=' ')val2[j]=' '+val2[j];
var
temp=val2[j].split(' ');
var
incr=0;
if(
temp[0]==''){
incr=1;
}
temp2=temp[incr].substring(0,1);
temp3=temp[incr].substring(1,temp[incr].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[incr]=temp2+temp3;
for (var
i=incr+1;i<temp.length;i++){
temp2=temp[i].substring(0,1);
temp2=temp2.toLowerCase();
temp3=temp[i].substring(1,temp[i].length);
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
}
if(
endSentence[j]==undefined)endSentence[j]='';
result2+=temp.join(' ')+endSentence[j];
}
if(
result2.substring(0,1)==' ')result2=result2.substring(1,result2.length);
document.forms[0].convert.value=result2;
}
//-->
</script>
</head>
<body>
<form>
<input size=50 name="convert" onkeyup="sCase()">
</form>
</body>
</html>

Cheers - Pit

baconbutty 08-13-2004 03:27 AM

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

baconbutty 08-13-2004 03:30 AM

Sorry Pittimann, my typo. I mean't Sentence case.

You have anticipated my reply.

Pittimann 08-13-2004 03:34 AM

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

GurusGuru 08-13-2004 04:22 AM

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?

Pittimann 08-13-2004 04:27 AM

Hi!

You're welcome! :)
Quote:

Sorry about case and caps.
No prob! It is just that in German, not only names but many words (eg all nouns) have to start with upper case; so nobody in Germay would need such a script and I only learned about sentence case when asking a person here with a similar request.

Regards - Pit

Pittimann 08-13-2004 04:32 AM

Hi!

In reply to your edit of the latest post you sent:
PHP Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html>
<
head>
<
title>Untitled</title>
<
script language="JavaScript" type="text/javascript">
<!--
function
sCase(who){
val=who.value;
result=new Array();
result2='';
count=0;
endSentence=new Array();
for (var
i=1;i<val.length;i++){
if(
val.charAt(i)=='.'||val.charAt(i)=='!'||val.charAt(i)=='?'){
endSentence[count]=val.charAt(i);
count++
}
}
var
val2=val.split(/[.|?|!]/);
if(
val2[val2.length-1]=='')val2.length=val2.length-1;
for (var
j=0;j<val2.length;j++){
val3=val2[j];
if(
val3.substring(0,1)!=' ')val2[j]=' '+val2[j];
var
temp=val2[j].split(' ');
var
incr=0;
if(
temp[0]==''){
incr=1;
}
temp2=temp[incr].substring(0,1);
temp3=temp[incr].substring(1,temp[incr].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[incr]=temp2+temp3;
for (var
i=incr+1;i<temp.length;i++){
temp2=temp[i].substring(0,1);
temp2=temp2.toLowerCase();
temp3=temp[i].substring(1,temp[i].length);
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
}
if(
endSentence[j]==undefined)endSentence[j]='';
result2+=temp.join(' ')+endSentence[j];
}
if(
result2.substring(0,1)==' ')result2=result2.substring(1,result2.length);
who.value=result2;
}
//-->
</script>
</head>
<body>
<form>
<input size=50 name="convert" onkeyup="sCase(this)">
<br>
<input size=50 name="convert2" onkeyup="sCase(this)">
</form>
</body>
</html>

Cheers - Pit

GurusGuru 08-13-2004 05:18 AM

Thanks again.

Pittimann 08-13-2004 05:23 AM

Hi!

No prob. ;)

Cheers - Pit

GurusGuru 09-09-2005 03:49 AM

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.