Function to capitalise each word

N

Nige

Repost - as some readers took got sidetracked.

To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after a
space or hyphen.

So many users seem to think that just because they are online they can
use addresses like "8 brown road, chatham, kent" and so I have to
manually change them.
 
E

Evertjan.

Nige wrote on 11 nov 2003 in comp.lang.javascript:
To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after
a space or hyphen.

A better result is a capital after any nonalphabetical char
[words like van, de in Dutch and l' in French, etc. need seperate coding]


<script>

function properCase(a) {
var b="";
var notyet=true;
for (i=0;i<=a.length;i++) {
m = a.substr(i,1)
b += (notyet)?m.toUpperCase():m.toLowerCase();
notyet = (m.toUpperCase() == m.toLowerCase())
}
return b
}

document.write(properCase("the bluE Fox O'bRian and hip-hap+hOP"));

</script>

This is analogous to my 2002 vbscript function in:

<http://groups.google.com/
groups?selm=Xns921ECA520FFABeejj99%40194.109.6.74>
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Repost - as some readers took got sidetracked.

To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after a
space or hyphen.

So many users seem to think that just because they are online they can
use addresses like "8 brown road, chatham, kent" and so I have to
manually change them.

This should, perhaps, be done with String.replace(RE, Fnc), using a
RegExp to isolate each word and a function to UpCase its first
character.

Here's another way, partially satisfactory :

S = "3 smith street"
S = S.split(/\W+/)
for (j=0; j<S.length;j++) S[j] =
S[j].substring(0,1).toUpperCase() + S[j].substring(1)
S.join(' ')

But how do you deal with Stoke-on-Trent or Bridge of Weir , let alone
s'Hertogenbosch ?
 
E

Evertjan.

Dr John Stockton wrote on 11 nov 2003 in comp.lang.javascript:
But how do you deal with Stoke-on-Trent or Bridge of Weir , let alone
s'Hertogenbosch ?

That "left alone" last one is easy:

s = "S'hertoGenbosch"
s = s.replace(/s'Hertogenbosch/i,"Den Bosch")
if (today=="carnaval")
s = s.replace(/Den Bosch/,"Oeteldonk")
 
N

Nige

In said:
Nige wrote on 11 nov 2003 in comp.lang.javascript:
To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after
a space or hyphen.

A better result is a capital after any nonalphabetical char
[words like van, de in Dutch and l' in French, etc. need seperate coding]


Thanks for your solution, I've now incorporated it into the form, but
rather than changing the value, I use it to alert the user how the text
should (probably) appear; a good compromise I think.
 
E

Evertjan.

Nige wrote on 26 nov 2003 in comp.lang.javascript:
In said:
Nige wrote on 11 nov 2003 in comp.lang.javascript:
To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after
a space or hyphen.

A better result is a capital after any nonalphabetical char
[words like van, de in Dutch and l' in French, etc. need seperate coding]


Thanks for your solution, I've now incorporated it into the form, but
rather than changing the value, I use it to alert the user how the text
should (probably) appear; a good compromise I think.

Show us the resulting URL
WHEN you are ready and IF possible.
 
N

Nige

In said:
newString=prompt("Capitalized\nAccept, change or refuse",sugString);

OK, I've tried it, and it nearly works! I'm having a problem if the user
clicks Cancel. This is the code I've used (apologies):

newString=prompt("Capitalized\nAccept, change or refuse",sugString);
if (sugString == null)
return "TESTING - should return me"
else return sugString

Also, I quite liked the way that alert() makes a sound, and also
positions the pop-up window close to the cursor, rather than at the edge
of the main window. Can this be changed?
 
E

Evertjan.

Nige wrote on 28 nov 2003 in comp.lang.javascript:
OK, I've tried it, and it nearly works! I'm having a problem if the
user clicks Cancel. This is the code I've used (apologies):

newString=prompt("Capitalized\nAccept, change or
refuse",sugString);
if (sugString == null)
return "TESTING - should return me"
else return sugString

1
I suppose you want to test the newString, as the sugString is input.

2
the "else" is superfluous.

Try this:

<script>

function AskForCaps(sugString, origString){
newString=prompt("Capitalized\n(change+)OK or cancel",sugString);
if (newString == null)
return origString
return newString
}


alert(AskForCaps("John Doe", "john doe")) // test

</script>


Also, I quite liked the way that alert() makes a sound, and also
positions the pop-up window close to the cursor, rather than at the
edge of the main window. Can this be changed?

No idea.
 
T

Thomas 'PointedEars' Lahn

Nige said:
Also, I quite liked the way that alert() makes a sound, and also
positions the pop-up window close to the cursor, rather than at
the edge of the main window. Can this be changed?

No, you describe features of a particular user agent (IE). My
Mozilla/5.0, e.g., although also running under Win2k,does neither make
the Windows alert sound (and *I* am thankful for that) nor does it
position such _dialogs_ (not really pop-up windows) near the cursor (it
positions it in the center of Navigator window instead). Also note that
a mouse device does not need to be present to navigate web documents, so
there does not need to be a mouse cursor that can be used for positioning.


PointedEars
 
M

Mark Szlazak

JavaScript The Definitive Guide - 4th Ed. pp 526, 527.

SYNOPSIS
string.replace(regexp, replacement)

DESCRIPTION
ECMAScript v3 specifies that the "replacement" argument to replace()
may be a function instead of a string, and this feature is implemented
in JavaScript 1.2 and JScript 5.5+. In this case, the function is
invoked for each match and the string it returns is used as the
replacement text. The first argument to the function is the string that
matched the pattern. The next arguments are the strings that matched
any parenthesized subexpressions within the pattern. There may be zero
or more of these arguments. The next argument is an integer that
specifies the position within "string" at which the match occurred, and
the final argument to the "replacement" function is the "string" itself.

EXAMPLE

To capitalize the first letter of all words in a string:

text.replace(/\b\w+\b/g, function(word) {
return word.substring(0,1).toUpperCase()
+ word.substring(1);
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top