Link to home
Start Free TrialLog in
Avatar of bburden
bburden

asked on

Need Sentence Case not ProperCase - Please

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.
Avatar of StormyWaters
StormyWaters
Flag of United States of America image

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
Avatar of Zvonko
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>

Avatar of bburden
bburden

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Avatar of bburden

ASKER

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
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
Avatar of bburden

ASKER

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.
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.