Solved

Need Sentence Case not ProperCase - Please

Posted on 2006-03-19
10
2,126 Views
Last Modified: 2012-05-05
I need a javascript that does Sentence Case as in MS Word.  Title or ProperCase caps the first letter of every word.  exp.. "This Is The Day.." - Sentence Case is "This is the day."

I have search in many forum and can not find wihat I am looking for.  I have found some samples you can put in Access of the after update, and by putting  a public function in a module. ProperCase Not Sentence Case.

I have found some examples as the one below to use in javascript.  This is the one I pefer.

<script language="javascript">
function firstLetterUpper(fml) {
    var pattern = /(\w)(\w*)/;
    var a = fml.value.split(/\s+/g);
    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern);
        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();
             a[i] = firstLetter + restOfWord;
    }
    fml.value = a.join(' ');
}
</script>

I just need it to just cap the first letter in the sentence and then after the peroid or question mark cap the first letter of the next sentence.

I am sure this can be done, but I can't seem to fiqure it out.

Thank you in advance.
0
Comment
Question by:bburden
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 13

Expert Comment

by:StormyWaters
ID: 16231552
function capitalize(str) {
  var re = /([?\.\!])( *)(\w)/g;
      str = str.replace(re, function(x){return RegExp.$1 + RegExp.$2 + RegExp.$3.toUpperCase()});
      return str.replace(/^./,function(x){return x.toUpperCase()}); //Capitalize first char, inelegantly.
      
}

Capitalizes any alphanumeric after ? or . or ! followed by any amount of spaces.

Very similar (in fact, a modified function) to http:Q_21779941.html
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16232290
Simmelar, but still different:

<script>

var theText = "This Is The first Day. This Is The second Day. This Is The third Day.";

theText = theText.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});

document.write(theText);
</script>

0
 

Author Comment

by:bburden
ID: 16232664
Hello all and thanks.  I guess I did not explain myself clearly, I am bad about that.  I need to associate the above javascript with a text box or text area.  If the text area is filled in incorrectly by the user, for example they type in all CAPS  I want it to convert to Sentence Case as the example I have listed.  I do not want every letter to be cap of each word.

Maybe I am just not getting what you guys are saying please forgive my ingorance.

<script language="javascript">
function firstLetterUpper(fml) {
    var pattern = /(\w)(\w*)/;
    var a = fml.value.split(/\s+/g);
    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern);
        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();
             a[i] = firstLetter + restOfWord;
    }
    fml.value = a.join(' ');
}
</script>

</head>

<body>
<form>
<b>
<p><input type="text" name="fml" size="30" onChange="return firstLetterUpper(this);"></p>
</b>
  <textarea name="textfield" cols="50" rows="4" onChange="return firstLetterUpper(this);"></textarea>

<b>
<p><input type="submit" value="Submit" name="B1" ID="Submit1">
</b>

Thanks again for all your help.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
ID: 16233639
And I guess you did not test my version:

function firstLetterUpper(fml) {
  fml.value = fml.value.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
}

0
 
LVL 11

Expert Comment

by:mvan01
ID: 16233672
Hi bburden,

Zvonko provided a top notch solution once again.  He provides a snippet to show how and/or that it works, then he's on to help 100s of other people.  You have working code in your example, and so you just need to use his solution in the same manner...

function sentenceCase(aForm) {
    var theText = aForm.value;
    theText = theText.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
    aForm.value = theText;
}

You can call that function the same as you called your proper case function.  Sweet.  I'd give him an A!

mvan
0
 
LVL 11

Expert Comment

by:mvan01
ID: 16233690
Look at that, he was back to show how it fit into your program ... quicker than I could type my message!

Thanks for sharing your wisdom, Zvonko.

mvan
0
 

Author Comment

by:bburden
ID: 16234820
I was never doubting Zvonko , I said it was just me not getting it.  That why I said in advance forgive for my ingornace.  I have given him a A grade and accepted his answer.  

I am not astute as most in here, but I do know how to appreciate help that is given.

Thanks again Zvonko
0
 
LVL 11

Expert Comment

by:mvan01
ID: 16237017
Sorry, I didn't mean to call your gratitude or appreciation into question.  I just meant that my putting it into a function wouldn't merit any me any credit - he did the real work ... and while I was typing my message, he went ahead and put it into a function too (I didn't see that before I hit the submit on mine).

mvan
0
 

Author Comment

by:bburden
ID: 16240583
All is well, Mvan, Everthing is Everthing. I do appreciate all you guys in here.  To everyone have a great week.

Later until the next time.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16241405
You are welcome.
And please consider splitting points to all experts who invested time and efforts to code the solution AND to explain the solution in plain English words to help you understand.

Thanks mvan.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
is this a cms? 8 57
replace all occurrences within a string 8 26
two layers of simple javascript validation 2 46
Converting dates in JavaScript 2 31
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now