www.webdeveloper.com
Results 1 to 13 of 13

Thread: Sentence case

  1. #1
    Join Date
    Dec 2002
    Posts
    112

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Derby, England
    Posts
    297
    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.

  3. #3
    Join Date
    Aug 2004
    Location
    Derby, England
    Posts
    297
    Sorry, I have just noted that you were looking at "Sentence caps". I probably have not answered your question.

  4. #4
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    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
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  5. #5
    Join Date
    Aug 2004
    Location
    Derby, England
    Posts
    297
    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

  6. #6
    Join Date
    Aug 2004
    Location
    Derby, England
    Posts
    297
    Sorry Pittimann, my typo. I mean't Sentence case.

    You have anticipated my reply.

  7. #7
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi baconbutty.

    It was not your typo.

    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
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  8. #8
    Join Date
    Dec 2002
    Posts
    112
    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?
    Last edited by GurusGuru; 08-13-2004 at 04:24 AM.

  9. #9
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    You're welcome!
    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
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  10. #10
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    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
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  11. #11
    Join Date
    Dec 2002
    Posts
    112
    Thanks again.

  12. #12
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    No prob.

    Cheers - Pit
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  13. #13
    Join Date
    Dec 2002
    Posts
    112
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles