#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Javascript Proper Case of Names


    I have a name column in my databse which i need to display in proper case(ex: "THIS IS SAMPLE" to "This Is Sample") making all first letter in words capital letter .
    I almost covered cases but my problem is how to handle second apostrophe

    Problem:
    Input--- (O'RELIEY'S)
    Expected Output---(O'Reliey's)
    Present Output---- (O'Reliey'S)


    Code:


    <!DOCTYPE html>
    <html>
    <head>
    <script>
    var str="SUMMIT MOTORS PTY LTD-ROCKDALE MCDONALD -nt (O'RELIEY'S) ";
    function toProperCase(s)
    {
    return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
    function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();} return $1.toUpperCase(); });
    }
    str=toProperCase(str);
    str=str.replace("Hmca","HMCA");
    str=str.replace("Kia","KIA");
    str=str.replace("Mobis","MOBIS");
    if(str.match("-Nsw") || str.match("Nsw"))
    {
    str=str.replace("Nsw","NSW");
    }
    if(str.match("-Wa") || str.match("Wa"))
    {
    str=str.replace("Wa ","WA ");
    }
    if(str.match("-Act") || str.match("Act"))
    {
    str=str.replace("Act ","ACT ");
    }
    if(str.match("-Nt") || str.match("Nt"))
    {
    str=str.replace("Nt ","NT ");
    }
    if(str.match("-Nt") || str.match("Nt"))
    {
    str=str.replace("Nt ","NT ");
    }
    document.write(str);

    </script>
    </head>
    <body>


    </body>
    </html>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Before we continue, why do you have to do this with Javascript? Surely it'd be easier with whatever server-side language you're using?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    True this could get tricky though, I think RegExp is the way to go.
    Last edited by Nanomech; July 5th, 2013 at 01:17 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0
    Actually I am BI Consultant i am using this code in my reports .I am using "Actuate BIRT" which is Business Intelligence reporting tool where in i can embed javascript code in my report. I have just created a sample and testing my possible name conditions coming from database. If this works im going to embed this code in my report.

    Thanks in advance.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0
    Hi Guys! I found solution for that now.Thanks every one.

    Soultion:After getting results im replacing 'S with 's
    here is the changed code :

    function toProperCase(s)
    {
    return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
    function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();}return $1.toUpperCase();}).replace(/'S/g, "'s")

    }


    complete Code:


    <!DOCTYPE html>
    <html>
    <head>
    <script>
    var str="SUMMIT MOTORS PTY LTD-ROCKDALE MCDONALD -nt (O'RELIEY'S) ";
    function toProperCase(s)
    {
    return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
    function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();}return $1.toUpperCase();}).replace(/'S/g, "'s")

    }

    str=toProperCase(str);
    str=str.replace("Hmca","HMCA");
    str=str.replace("Kia","KIA");
    str=str.replace("Mobis","MOBIS");
    if(str.match("-Nsw") || str.match("Nsw"))
    {
    str=str.replace("Nsw","NSW");
    }
    if(str.match("-Wa") || str.match("Wa"))
    {
    str=str.replace("Wa ","WA ");
    }
    if(str.match("-Act") || str.match("Act"))
    {
    str=str.replace("Act ","ACT ");
    }
    if(str.match("-Nt") || str.match("Nt"))
    {
    str=str.replace("Nt ","NT ");
    }
    if(str.match("-Nt") || str.match("Nt"))
    {
    str=str.replace("Nt ","NT ");
    }
    document.write(str);

    </script>
    </head>
    <body>


    </body>
    </html>

IMN logo majestic logo threadwatch logo seochat tools logo