'Capitalize first and third letter in names like McArthur or McMinn
Is there a way to identify compound names and correctly capitalize First and Third character?
While changing case is a trivial task using CSS, when dealing with compound names, I havent found any straight forward solution.
One thing that comes to mind is a use of some sort of look-up to identify names such as LeBare, or McDonalds, McArthur etc and then apply some logic to change the case ... anyone aware of any other viable solutions that might be out there so I wont have to reinvent the wheel (so to speak)?
Note: Users will be entering these names directly in a text box. There is no guarantee that a user will not enter something like mcMinn or macArthur etc. Having said that, I am also noticing names with "Le" and "La" prefixes (these are the names that have latin/spanish roots perhaps, I could be wrong though).
Solution 1:[1]
One thing to remember during this process is that you could also have the case of "MacArthur" where it would be the 1st and 4th character. Depending on where you pull your data from (user input, database, etc.) I would say using it as is could be an option. However, if the name comes in as all lower case then you may have to compile a list of known name prefixes and check against it. My point is, unless the names come in as all lower case and will be displayed to a user I would just use the original formatting.
Solution 2:[2]
You can't make it only with CSS. There are several "lettering" jQuery plug-in such as http://letteringjs.com/. You can put your own logic based on names in jQuery and style each letter with lettering library like
.target.char1, .target.char3 {
text-transform:uppercase;
}
Solution 3:[3]
You use like this by Jquery.
$("#LastName").keyup(function () {
var last_name = $("#LastName").val();
var op = last_name.substr(0, 2);
if (op == "mc" || op == "Mc" || op == "MC") {
$("#LastName").val("Mc" + (last_name.charAt(2).toUpperCase()) + last_name.substr(3).toLowerCase());
} else {
$("#LastName").val(last_name.charAt(0).toUpperCase() + last_name.substr(1).toLowerCase());
}});
Solution 4:[4]
You could loop through using index in javascript:
var inputValue = "mcminn";
var transformedInput = '';
if(inputValue){
for(var i=0; i<inputValue.length; i++){
if(i===0 || i=== 2){
transformedInput += inputValue.charAt(i).toUpperCase();
} else {
transformedInput += inputValue.charAt(i).toLowerCase();
}
}
}
console.log(transformedInput);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Sanderson |
| Solution 2 | Won |
| Solution 3 | rvchauhan |
| Solution 4 | benson23 |
