Solved

Need Sentence Case not ProperCase - Please

Posted on 2006-03-19
10
2,122 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:mvan01
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Auto Submit on dropdown box 3 32
Run Script only if a div is in the viewport 6 28
JavaScript error 1 33
Jquery autocomplete 10 19
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

771 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