/    Sign up×
Community /Pin to ProfileBookmark

Javascript that automatically capitalizes certain letters such as "McC" or "O’Shea"

Hi there,

I’m currently working with forms that use javascript – toUppercase() function for the last name. However, there are exceptions I run into. A user may have last names such as:

McCormick
O’Sullivan
O’Shea

I need assistance creating javascript that automatically capitalizes certain letters such as last names beginning with “Mc” or “O'”

Could someone help me with this?

Thanks so much!

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 09.2015 — Might not be perfect as I've only tested the supplied names ...

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Modify certain names </title>
</head>
<body>

<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

var specialNames = ["McC","O'S"];
var exampleNames = "Law Firm of: mccormick, o'sullivan and o'shea";

function checkNames(origStr) {
var alteredStr = origStr.toLowerCase(), pos, len;
for (var i=0; i<specialNames.length; i++) {
do {
pos = alteredStr.indexOf(specialNames[i].toLowerCase());
if (pos != -1) {
len = specialNames[i].length;
origStr = origStr.substr(0,pos)+specialNames[i]+origStr.substr(pos+len);
alteredStr = alteredStr.substr(0,pos)+specialNames[i]+alteredStr.substr(pos+len); // alert(alteredStr);
}
} while (pos != -1);
}
return origStr;
}
alert(exampleNames+'nn'+checkNames(exampleNames));

</script>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@schow2015authorJul 09.2015 — Thanks so much!

Might not be perfect as I've only tested the supplied names ...

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Modify certain names </title>
</head>
<body>

<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

var specialNames = ["McC","O'S"];
var exampleNames = "Law Firm of: mccormick, o'sullivan and o'shea";

function checkNames(origStr) {
var alteredStr = origStr.toLowerCase(), pos, len;
for (var i=0; i<specialNames.length; i++) {
do {
pos = alteredStr.indexOf(specialNames[i].toLowerCase());
if (pos != -1) {
len = specialNames[i].length;
origStr = origStr.substr(0,pos)+specialNames[i]+origStr.substr(pos+len);
alteredStr = alteredStr.substr(0,pos)+specialNames[i]+alteredStr.substr(pos+len); // alert(alteredStr);
}
} while (pos != -1);
}
return origStr;
}
alert(exampleNames+'nn'+checkNames(exampleNames));

</script>
</body>
</html>
[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@JMRKERJul 09.2015 — You're most welcome.

Happy to help.

There may be shorter/faster versions using regular expressions (regex).

Good Luck!

?

EDIT:

Alternate version with RegEx. Appears to work the same for values tested ...

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Modify certain names </title>
</head>
<body>

<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

var specialNames = ["McC","O'S","MacD","McD"];
var exampleNames = "Law Firm of: macDonald, mccormick, mcdavid, o'sullivan and o'shea";

function checkNames(origStr) {
var myRegExp;
for (var i=0; i<specialNames.length; i++) {
myRegExp = new RegExp(specialNames[i],'ig');
origStr = origStr.replace(myRegExp,specialNames[i]);
} return origStr;
}

alert(exampleNames+'nn'+checkNames(exampleNames));

</script>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@jalarieJul 23.2015 — What about names like MacGregor? What do you expect when the input word is "machinery"?
Copy linkTweet thisAlerts:
@Kevin2Jul 23.2015 — What do you expect when the input word is "machinery"?[/QUOTE]

Ha! Years ago (back in the '70s) there was a book titled "Sir Machinery" (read [B]MacHinery[/B] ?). Read it as a youth.

http://www.goodreads.com/book/show/2089659.Sir_Machinery

Good point though. Just when you think you've covered all the bases, the exception to the rule ruins everything. ?
Copy linkTweet thisAlerts:
@jalarieAug 04.2015 — I thought this looked like a fun project, so I wrote it in PERL, translated it into JavaScript, and got it to work with: D Eath, D'eath, d'Annunzio, da Vinci, de' Medici, de Medicis, DeLorean, du Barry, DuPont, Macedonia, Machinery, Macgregor, Mack, Macready, MacDonald, M'Carthy, M'grego, McDonald, Mac Dowell, MacLeod, MacPhall, Macpherson, Macquarie, O'Connor, O'reilly, van der Waals, von Trapp, Smith IV, Smith Sr , Smith Sr. , Smith Jr , Smith Jr., AvariJade, D'Jahnte, MarZette, MyAsia, Damarion, and DaMarion.

Those last two (Damarion and DaMarion) are boy and girl. Yes, they BOTH come out correctly.

This is all done with regular expressions. And it's not all that difficult.
Copy linkTweet thisAlerts:
@JMRKERAug 04.2015 — How many regular expressions did you use?
Copy linkTweet thisAlerts:
@taitrochoifreeAug 04.2015 — I also have the need to use your voice without knowing lam
Copy linkTweet thisAlerts:
@jalarieAug 04.2015 — How many regular expressions did you use?[/QUOTE]

I have 30 regular expressions to handle all variations of the names that I listed.
Copy linkTweet thisAlerts:
@JMRKERAug 07.2015 — ...

Those last two (Damarion and DaMarion) are boy and girl. Yes, they BOTH come out correctly.

This is all done with regular expressions. And it's not all that difficult.[/QUOTE]


What do the last 2 regular expressions look like and how are they different?
Copy linkTweet thisAlerts:
@jalarieAug 07.2015 — Some names, like those last two, are handled individually. To get those to work, I checked to make sure that the entered name is not all capitals, but contains at least one capital after the first character, and then return it as-is. If you enter "DaMarion" that's what comes back. Any other version, all caps, all lower-case, or inital capital only, produces "Damarion".
×

Success!

Help @schow2015 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...