Link to home
Start Free TrialLog in
Avatar of Gizeh2000
Gizeh2000

asked on

Javascript function needed to capitalize lastnames!

I need a javascript function which converts lastnames to correct capitalization.

For instance when I have a textfield called lastname and type in the name van helsing and leave the field it will be converted to for instance van Helsing.

Things to consider are :

I have a list of prefixes which need to checked upon so it will have to be included in the javascript function so I can easily add or remove items from the list and the function still has to work without too much hassle. (array,...)

List of prefixes
----------------
't, a, aan den, ad, al, ali, anh, bij de, bing, ch, d', da, de, de la, del, den, der, dinn, dos, du, e, el, het, hua, hong, huu, in 't, in de, in den, l', la, le, minh, nu, o', op, op 't, op de, op den, op het, oude, ta, te, ten, ter, thi, uit het, van, van 't, van de, van den, van der, van het, van ter, van 't, van de, van der, van het, van ter, von, von der, voor de, voor den.

Following examples should give you a clue about how names should like look after the function has been run on the lastname field

prefixes always have to be in small caps in the lastname field

Helsing
de Helsing
den Helsing
van den Helsing
Helsing Jones
Helsing de Jones
...
de Helsing Jones
de Helsing de Jones
...

Note that in some cases a little bit smarter programming is required because the '-' symbol is sometimes used.
Helsing-Jones
Helsing-de Jones
...
de Helsing-Jones
de Helsing-van Jones

de Helsing-Vanakker (note that the van here is not a prefix so please include things like this in your checks)

Also there are fields on my form which hold the how to address a person in a letter for instance which have a different set of rules

Rules here are that with this field : If nothing precedes the prefixes then only the first prefix gets a capital letter and ofcourse the lastname itself. (Dear mr Van Helsing)
When however something does precede the suffixes all prefixes remain small. (Dear mr Jones van Helsing)

I will include a short snippet of code so you can sorta see how it is supposed to work...I say sorta see cause this function doesnt work correct all the times the way I have described above.

<script language="javascript">
    function Capitalize(){    
                        var suffixes= false;
                                    
                  
                  lastname= document.EntryForm.achternaam.value;
                  
                  var FirstChar = lastname.substr(0,1);
                  var FirstCharU1 = FirstChar.toUpperCase();
                  var OtherChars;
                  var Completename;
                  
                  //alert(FirstCharU1);
                  
                  var h = lastname.lastIndexOf(' ');
                                    
                  if (h > 0) {
                        var x = lastname.toLowerCase();
                     suffixes= true;
                        FirstChar = x.substr(h+1,1);
                        var FirstCharU = FirstChar.toUpperCase();
                        OtherChars = x.substr(0, h+1) + FirstCharU + x.substr(h+2);
                        lastname= OtherChars;
                  }
                  
                  CompleteName = FirstCharU1 + achternaam.substr(1);
                  alert(CompleteName);
                  if (h == -1) {
                        lastname= CompleteName;
                  }                  
                  
                  document.EntryForm.pers_lastname.value = CompleteName;                                                        
            }    
      </script>


<form name="EntryForm" method="post">
lastname:<input type="text" name="achternaam" id="achternaam" size=30 onchange="Capitalize();">
output:<input type="text" name="pers_lastname" id="pers_lastname" size=30 value="">
</form>

This question is not that difficult but javascript just isn't my thing and this is kinda urgent so...I will be watching this thread so if there are any questions just ask.

Sincerely,
Gizeh

ASKER CERTIFIED SOLUTION
Avatar of MrRooster
MrRooster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gizeh2000
Gizeh2000

ASKER

Hiya Ian,

Sorry for the wait but since I can't check it right now...will have to be tomorrow when I start work again. (12-05)

I will check it out first thing tomorrow morning and will get back to you...thnx for the reply anyways.

Sincerely,
G
Worked like a charm Ian...thx again

Sincerely,
G