Javascript function needed to capitalize lastnames!

Posted on 2005-05-10
Last Modified: 2008-01-09
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

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-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;
                  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);
                  if (h == -1) {
                        lastname= CompleteName;
                  document.EntryForm.pers_lastname.value = CompleteName;                                                        

<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="">

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.


Question by:Gizeh2000
    LVL 2

    Accepted Solution

    This might be some use, gives me the following output:-

    DE HELSING:de Helsing
    DEN HELSING:den Helsing
    VAN DEN HELSING:van den Helsing
    HELSING JONES:Helsing Jones
    HELSING DE JONES:Helsing de Jones
    DE HELSING JONES:de Helsing Jones
    DE HELSING DE JONES:de Helsing de Jones
    HELSING-DE JONES:Helsing-de Jones
    DE HELSING-JONES:de Helsing-Jones
    DE HELSING-VAN JONES:de Helsing-van Jones
    DE HELSING-VANAKKER:de Helsing-Vanakker

    <------------------ CUT
    function sortName(n) {
          var ar1,ar2,x,y,str,out,out2;
          var prefixes=new Array("'t", "a", "aan", "ad", "al", "ali", "anh", "bij", "bing", "ch", "d'", "da", "de", "de", "del", "den", "der", "dinn", "dos", "du", "e", "el", "het", "hua", "hong", "huu", "in", "in", "in", "l'", "la", "le", "minh", "nu", "o'", "op", "op", "op", "op", "op", "oude", "ta", "te", "ten", "ter", "thi", "uit", "van", "von", "von", "voor");
          var smallprefixes = new Array();

          // load the prefixes
          for (x in prefixes) {

          ar1 = new String(n).split(" ");
          for (x in ar1) {
                ar2 = ar1[x].split("-");
                for (y in ar2) {
                      if (smallprefixes[str]==null) {
                            // isn't in our list of prefixes so capitalise it
                      } else {
                      out = out+"-"+ar2[y];
                if (out.substring(0,1)=="-") out=out.substring(1);
                out2=out2+out+" ";
          if (out2.length>0) out2=out2.substring(0,out2.length-1);
          return out2;
    document.write("<b>DE HELSING:"+sortName("DE HELSING")+"</b><br>");
    document.write("<b>DEN HELSING:"+sortName("DEN HELSING")+"</b><br>");
    document.write("<b>VAN DEN HELSING:"+sortName("VAN DEN HELSING")+"</b><br>");
    document.write("<b>HELSING JONES:"+sortName("HELSING JONES")+"</b><br>");
    document.write("<b>HELSING DE JONES:"+sortName("HELSING DE JONES")+"</b><br>");
    document.write("<b>DE HELSING JONES:"+sortName("DE HELSING JONES")+"</b><br>");
    document.write("<b>DE HELSING DE JONES:"+sortName("DE HELSING DE JONES")+"</b><br>");
    document.write("<b>HELSING-DE JONES:"+sortName("HELSING-DE JONES")+"</b><br>");
    document.write("<b>DE HELSING-JONES:"+sortName("DE HELSING-JONES")+"</b><br>");
    document.write("<b>DE HELSING-VAN JONES:"+sortName("DE HELSING-VAN JONES")+"</b><br>");
    document.write("<b>DE HELSING-VANAKKER:"+sortName("DE HELSING-VANAKKER")+"</b><br>");</script>
    <-------------- CUT



    Author Comment

    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.


    Author Comment

    Worked like a charm Ian...thx again


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now