views:

627

answers:

4

How to give sentence case to sentences through CSS or javascript?

I've tried these CSS properties but these are different

capitalize    Transforms the first character of each word to uppercase
uppercase   Transforms all characters to uppercase
lowercase   Transforms all characters to lowercase

Edit: 19 FEB 2010

is there any option in jquery to achieve this?

+2  A: 

It's not possible. Your will need to either properly case the text server-side, or use JavaScript.

Alex Barrett
Oh, its possible. The hard part is determining which content bits are sentences and which bits are like option values, labels, or so forth. So, he would have to segregate the appropriate content containers from the prior mentioned elements and then parse out intermittent HTML tags. Then he could scan the content for sentence beginnings and endings, assuming it is correctly punctuated, and then he could capitalize the sentences assuming he can navigate around proper named abbreviations. Then he has to return the content the way he found with HTML tags reinserted. JavaScript can do that, but....
A: 

Something similar to this will work in JavaScript:

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

}

Won't work perfectly in ALL cases, but, it might get you somewhere. There are a lot more elegant solutions on back-end languages, typically, though.

KyleFarris
pls give me a good js solution and tell me how to add
metal-gear-solid
Three problems.1) Your solution is perfect for text that is actually sentences, but can improperly interfere with text that is not sentences.2) You are passing a function through a replace method. This is the most serious non-AJAX security flaw in JavaScript as it can be used to execute malicious code if the code base is compromised on the server. Compromising such code from legitimate web servers is common, low complexity, and rarely patched.3) You have a typo. The semicolon on the top line should not exist.
@austin, could u explain 2 again? Didn't understand that at all... How is passing a function to the replace() method a security concern???
J-P
@austin: 1) I actually even said that it won't work in all cases. It's a horrible solution especially when you consider that it's a horrible idea in the first place. Just thought I would answer the question. 2) I'm not passing a function to a replace, I'm passing a string to the replace method which uses a callback to manipulate the text. This is a very common and not a security issue. It's merely replacing the first character in the string to be uppercase. Maybe I don't quite get it but I don't see any chance of Cross-site-scripting or anything like that using something like this. 3) Fixed.
KyleFarris
You are passing an anonymous function with an argument as the second argument of the replace method. When passing a function through a replace method you are creating a hole by which malicious code can be piped in and automatically executed without an explicit call to the malicious code. Considering all JavaScript is executed as text through an eval method at the interpreter it is completely open internally to injection attacks. String injection in JavaScript is not typically a problem as it usually just causes the code to crash due to syntax violation or namespace collision.
i'm not clear on this - why wouldn't the method ALWAYS be called? where's the opportunity to inject just because it's an anonymous method? I assume you're thinking of a string like 'foo", <malicious code here>' but I don't see why the second item being an anonymous method call makes this any worse.
aronchick
+3  A: 

Don't use JavaScript to fix your content. This is inefficient on a scale of ridiculous. Write your content correctly before you publish it or use some coding scheme on the server side. If this is some scheme to fix content that you don't control, such as user supplied, then simply state the content comes from your users and not you.

Seriously, this is going to delay the loading of your page significantly and cause visitors to abandon your site.

A: 

what about:

str = "HeLlo Its aamiR here";
str = str.substr(0,1).toUpperCase()+str.substr(1).toLowerCase()
AamirAfridi.com