• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1202
  • Last Modified:

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

0
Gizeh2000
Asked:
Gizeh2000
  • 2
1 Solution
 
MrRoosterCommented:
This might be some use, gives me the following output:-

HELSING:Helsing
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-JONES:Helsing-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
<html>
<body>
<script>
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) {
            smallprefixes[prefixes[x].toLowerCase()]=1;
      }

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


cheers,

Ian
0
 
Gizeh2000Author Commented:
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
0
 
Gizeh2000Author Commented:
Worked like a charm Ian...thx again

Sincerely,
G
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now