Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper Case - Mc - Mac - Spaces

Status
Not open for further replies.

adamroof

Programmer
Joined
Nov 5, 2003
Messages
1,107
Location
US
im sure this can be expanded on, cleaned up, and adjusted, but i searched here and google, noone had it completed yet.

In a nutshell, names converted to proper case with handling for spacing (just 1 right now) and McDonald and MacDonalds of the world

Any other anomoly you guys can think of please let me know.

Code:
function nameCase(val){
    var name = val.value.toLowerCase();
    var spc = 0    
    if (name.indexOf(' ') > 0) {
        spc = name.indexOf(' ');        
        var name1 = name.slice(0,1).toUpperCase() + name.slice(1,spc).toLowerCase();        
        var name2 = name.slice(spc + 1,spc + 2).toUpperCase();
        var name3 = name.slice(spc + 2).toLowerCase();
        val.value = name1 + ' ' + name2 + name3;
    }
    else {
        var name4 = name.slice(0,2);
        var name5 = name.slice(0,3);
        if (name4 == 'mc' || name5 == 'mac'){
            spc = name.indexOf('c') + 1;
            var name1 = name.slice(0,1).toUpperCase() + name.slice(1,spc).toLowerCase();      
            var name2 = name.slice(spc,spc + 1).toUpperCase();
            var name3 = name.slice(spc + 1).toLowerCase();
            val.value = name1 + name2 + name3;
        }
        else {
            val.value = name.slice(0,1).toUpperCase() + name.slice(1,name.length).toLowerCase();
        }
    };
}

useage - <input type=text onblur="nameCase(this);" />
 
I've not tested this, but it looks like the "Mc"/"Mac" code only works when "Mc"/"Mac" are the first 2/3 letters, so "ronald mcdonald" would not work.

Perhaps splitting the string on any spaces, and passing each array element into your conversion function would be a quicker way. Then, run the "Mc"/"Mac" test over each of them, and if not found, simply convert the first letter to uppercase. Then join the array on spaces.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
well this is for a FirstName input and LastName input form, so that "shouldnt" happen

i was thinkin of splitting the first name box so if they typed first and last in same box it would put the last part in the last name box, but what if someones name is

billy ray cyrus = works, propers whole first name, but cant move ray to last


i guess you can check it out, just please dont submit the form, it is a live site...



aprreciate the feedback
 
Your mac routine will have false positives:
Macchi, Mace, Macedo, Macek, Macey, Mach, Machado, Machnik, and many more are examples of names beginning with Mac that should have only one capital letter.

Other names that have multiple caps are the various saints: StClair, StCroix, StMartin. Other variations of the first example include: St.Clair, St-Clair, St Clair, Saint Clair.

There are other names that use multiple caps, but I forget which ones.

Good luck,
Larry
 
well i guess the main reason to even start this is because there were so many form submissions in all CAPS and we all know what ALL CAPS means right?

so i lowercased the email addresses, and when i was at it, headed into this...the majority of the 5K records arent like you stated above, however who knows.

cant we just start an ICANN for human names too?

thanks for continual input...

902.adam.1354-2
 
O'Brian is another problem.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top