Function to capitalise each word

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nige

    Function to capitalise each word

    To save me re-inventing the wheel, does anyone have a function to
    capitalise each word in form data? I was thinking along the lines of
    capitalise the first letter of the string, then any other letter after a
    space or hyphen.

    So many users seem to think that just because they are online they can
    use names like "mr john smith" and so I have to manually change them.


    --
    Nige

    Please replace YYYY with the current year
    ille quis mortem cum maximus ludos, vincat
  • Lee

    #2
    Re: Function to capitalise each word

    Nige said:[color=blue]
    >
    >To save me re-inventing the wheel, does anyone have a function to
    >capitalise each word in form data? I was thinking along the lines of
    >capitalise the first letter of the string, then any other letter after a
    >space or hyphen.
    >
    >So many users seem to think that just because they are online they can
    >use names like "mr john smith" and so I have to manually change them.[/color]

    There is no way of knowing which letters in a person's name
    should be capitalized. If your user chooses to be called
    "john smith", why do you feel that you have to change it?

    Comment

    • Disco

      #3
      Re: Function to capitalise each word

      Nige wrote:[color=blue]
      > To save me re-inventing the wheel, does anyone have a function to
      > capitalise each word in form data? I was thinking along the lines of
      > capitalise the first letter of the string, then any other letter
      > after a space or hyphen.
      >
      > So many users seem to think that just because they are online they can
      > use names like "mr john smith" and so I have to manually change them.[/color]

      <p style="text-transform:capit alize">mr john smith</p>






      Comment

      • Jim Ley

        #4
        Re: Function to capitalise each word

        On 10 Nov 2003 10:07:21 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
        [color=blue]
        >Nige said:[color=green]
        >>
        >>To save me re-inventing the wheel, does anyone have a function to
        >>capitalise each word in form data? I was thinking along the lines of
        >>capitalise the first letter of the string, then any other letter after a
        >>space or hyphen.
        >>
        >>So many users seem to think that just because they are online they can
        >>use names like "mr john smith" and so I have to manually change them.[/color]
        >
        >There is no way of knowing which letters in a person's name
        >should be capitalized. If your user chooses to be called
        >"john smith", why do you feel that you have to change it?[/color]

        Or people such as "Aravinda de Silva" who you'd be getting the name
        completely wrong if you use De.

        Jim.

        --
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Fabian

          #5
          Re: Function to capitalise each word

          Nige hu kiteb:
          [color=blue]
          > To save me re-inventing the wheel, does anyone have a function to
          > capitalise each word in form data? I was thinking along the lines of
          > capitalise the first letter of the string, then any other letter
          > after a space or hyphen.
          >
          > So many users seem to think that just because they are online they can
          > use names like "mr john smith" and so I have to manually change them.[/color]

          In the case of my surname (van-de-l'Isle), this would result in an
          incorrect capitalisation. Banks have lost my account over this kind of
          thing. Then there was the time the bank's computer code decided that a
          hyphen ended the surname string. Boy was I pished off over that one.

          --
          --
          Fabian
          Visit my website often and for long periods!


          Comment

          • Fabian

            #6
            Re: Function to capitalise each word

            Fabian hu kiteb:
            [color=blue]
            > Nige hu kiteb:
            >[color=green]
            >> To save me re-inventing the wheel, does anyone have a function to
            >> capitalise each word in form data? I was thinking along the lines of
            >> capitalise the first letter of the string, then any other letter
            >> after a space or hyphen.
            >>
            >> So many users seem to think that just because they are online they
            >> can use names like "mr john smith" and so I have to manually change
            >> them.[/color]
            >
            > In the case of my surname (van-de-l'Isle), this would result in an
            > incorrect capitalisation. Banks have lost my account over this kind of
            > thing. Then there was the time the bank's computer code decided that a
            > hyphen ended the surname string. Boy was I pished off over that one.[/color]

            If you really want to check this kind of thing, you could have the
            submit linked to a function that gives the user a lst chance to edit
            that field before submitting.

            "The name field appears to have unusual capitalisation. Is this correct,
            or do you want to go back? Submit/Edit"

            But forcing this field into your idea of correct will really annoy some
            people.


            --
            --
            Fabian
            Visit my website often and for long periods!


            Comment

            • Andrew DeFaria

              #7
              Re: Function to capitalise each word

              Jim Ley wrote:
              [color=blue]
              > On 10 Nov 2003 10:07:21 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
              >[color=green]
              >> Nige said:
              >>[color=darkred]
              >>> To save me re-inventing the wheel, does anyone have a function to
              >>> capitalise each word in form data? I was thinking along the lines of
              >>> capitalise the first letter of the string, then any other letter
              >>> after a space or hyphen.
              >>>
              >>> So many users seem to think that just because they are online they
              >>> can use names like "mr john smith" and so I have to manually change
              >>> them.[/color]
              >>
              >> There is no way of knowing which letters in a person's name should be
              >> capitalized. If your user chooses to be called "john smith", why do
              >> you feel that you have to change it?[/color]
              >
              > Or people such as "Aravinda de Silva" who you'd be getting the name
              > completely wrong if you use De.[/color]

              And yet with my last name I prefer DeFaria.

              If the user cannot spell their own name then IMHO, that's their problem
              - not yours!
              --
              Why do people ask "Can I ask you a question?".... Didn't really give me
              a choice there, did ya sunshine?

              Comment

              • Nige

                #8
                Re: Function to capitalise each word

                In comp.lang.javas cript, Lee wrote:
                [color=blue]
                > If your user chooses to be called
                >"john smith", why do you feel that you have to change it?[/color]

                I used it as an example, the main thing I want to change is their
                address: "21 park street, birmingham" to "21 Park Street, Birmingham"


                --
                Nige

                Please replace YYYY with the current year
                ille quis mortem cum maximus ludos, vincat

                Comment

                • Fabian

                  #9
                  Re: Function to capitalise each word

                  Nige hu kiteb:
                  [color=blue]
                  > In comp.lang.javas cript, Lee wrote:
                  >[color=green]
                  >> If your user chooses to be called
                  >> "john smith", why do you feel that you have to change it?[/color]
                  >
                  > I used it as an example, the main thing I want to change is their
                  > address: "21 park street, birmingham" to "21 Park Street, Birmingham"[/color]

                  Then you picked just about the worst possible example. Many people are
                  sensitive about having their name mispelt. For your company to mispell a
                  customer's name when they entered it correctly is a surefire way to lose
                  that customer. And that kind of customer tells ten people all about it.
                  Unless they are online, in which case they tell a hundred.

                  --
                  --
                  Fabian
                  Visit my website often and for long periods!



                  Comment

                  • Fox

                    #10
                    Re: Function to capitalise each word


                    emulating php ucwords function:

                    function
                    ucwords(str)
                    {

                    return str.replace(/\b\w/gi,
                    function(c,i,s) {
                    return c.toUpperCase() ;
                    });

                    }

                    OR (more js-like)

                    String.prototyp e.ucwords = function(){
                    return this.replace(/\b\w/gi, function(c,i,s) { return
                    c.toUpperCase() ; });
                    }

                    example:

                    var properAddress = rawaddress.ucwo rds();

                    all problems mentioned in this thread will still exist... handle with care.



                    Nige wrote:[color=blue]
                    >
                    > To save me re-inventing the wheel, does anyone have a function to
                    > capitalise each word in form data? I was thinking along the lines of
                    > capitalise the first letter of the string, then any other letter after a
                    > space or hyphen.
                    >
                    > So many users seem to think that just because they are online they can
                    > use names like "mr john smith" and so I have to manually change them.
                    >
                    > --
                    > Nige
                    >
                    > Please replace YYYY with the current year
                    > ille quis mortem cum maximus ludos, vincat[/color]

                    Comment

                    • Evertjan.

                      #11
                      Re: Function to capitalise each word

                      Fox wrote on 11 nov 2003 in comp.lang.javas cript:
                      [color=blue]
                      > String.prototyp e.ucwords = function(){
                      > return this.replace(/\b\w/gi, function(c,i,s) { return
                      > c.toUpperCase() ; });
                      > }
                      >[/color]

                      String.prototyp e.ucwords = function(){
                      return this.toLowerCas e().replace(/\b\S/g, function(x) { return
                      x.toUpperCase() ; });
                      }

                      The rest has to be lowercase, IMHO.

                      I do not think the i in /gi is necessary.

                      Why replace the whole word?

                      Please explain for us mortals what the (c,i,s) does !


                      --
                      Evertjan.
                      The Netherlands.
                      (Please change the x'es to dots in my emailaddress)

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: Function to capitalise each word

                        "Evertjan." <exjxw.hannivoo rt@interxnl.net > writes:
                        [color=blue]
                        > Fox wrote on 11 nov 2003 in comp.lang.javas cript:
                        >[color=green]
                        >> String.prototyp e.ucwords = function(){
                        >> return this.replace(/\b\w/gi, function(c,i,s) { return
                        >> c.toUpperCase() ; });
                        >> }
                        >>[/color]
                        >
                        > String.prototyp e.ucwords = function(){
                        > return this.toLowerCas e().replace(/\b\S/g, function(x) { return
                        > x.toUpperCase() ; });
                        > }[/color]

                        Your version would uppercase[1] any non-space letter after a word
                        boundary. The previous version uppercased all word characters after
                        a word boundary. Since word characters includes all letters, and only
                        letters can be meaninfully uppercased, there isn't any significant difference.
                        [color=blue]
                        > The rest has to be lowercase, IMHO.[/color]

                        What rest? Your RegExp matches more characters than the previous one.
                        [color=blue]
                        > I do not think the i in /gi is necessary.[/color]

                        Correct, \w matches both upper- and lowercase letters already.
                        [color=blue]
                        > Why replace the whole word?[/color]

                        It doesn't, it only replaces one character at a time. The \w code
                        matches *one* word character, not an entire word.
                        [color=blue]
                        > Please explain for us mortals what the (c,i,s) does ![/color]

                        From ECMA-262 section 15.5.4.11:
                        ---
                        If /replaceValue/ is a function, then for each matched substring, call
                        the function with the following m + 3 arguments. Argument 1 is the
                        substring that matched. If /searchValue/ is a regular expression, the
                        next m arguments are all of the captures in the /MatchResult/ (see
                        15.10.2.1). Argument m + 2 is the offset within /string/ where the
                        match occurred, and argument m +3 is /string/. The result is a string
                        value derived from the original input by replacing each matched
                        substring with the corresponding return value of the function call,
                        converted to a string if need be.
                        ---
                        I.e.,
                        c is the current match
                        and since the regExp doesn't contain capturing parentheses (m==0),
                        i is the offset of the match in the original string, and
                        s is the original string.

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Evertjan.

                          #13
                          Re: Function to capitalise each word

                          Lasse Reichstein Nielsen wrote on 11 nov 2003 in comp.lang.javas cript:[color=blue]
                          > It doesn't, it only replaces one character at a time. The \w code
                          > matches *one* word character, not an entire word.[/color]

                          Rereading the specs ....., Yes, true.

                          [color=blue][color=green]
                          >> Please explain for us mortals what the (c,i,s) does ![/color]
                          >
                          > From ECMA-262 section 15.5.4.11:
                          > ---
                          > If /replaceValue/ is a function, then for each matched substring, call
                          > the function with the following m + 3 arguments. Argument 1 is the
                          > substring that matched. If /searchValue/ is a regular expression, the
                          > next m arguments are all of the captures in the /MatchResult/ (see
                          > 15.10.2.1). Argument m + 2 is the offset within /string/ where the
                          > match occurred, and argument m +3 is /string/. The result is a string
                          > value derived from the original input by replacing each matched
                          > substring with the corresponding return value of the function call,
                          > converted to a string if need be.
                          > ---
                          > I.e.,
                          > c is the current match
                          > and since the regExp doesn't contain capturing parentheses (m==0),
                          > i is the offset of the match in the original string, and
                          > s is the original string.[/color]

                          That is all new to me, very usefull.

                          Quick try:

                          s = "a b c d e f".replace(/\w/g, function(c,i,s)
                          { return "&lt;"+c+"="+i+ "+"+s+"<br> "});

                          document.write( s);

                          Nice.

                          Do you realy have to specify any not used arguments in function(c,i,s) ?


                          --
                          Evertjan.
                          The Netherlands.
                          (Please change the x'es to dots in my emailaddress)

                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: Function to capitalise each word

                            "Evertjan." <exjxw.hannivoo rt@interxnl.net > writes:
                            [color=blue]
                            > Do you realy have to specify any not used arguments in function(c,i,s) ?[/color]

                            No. Javascript functions generally don't have a fixed arity - you can
                            pass any number of arguments to any function. The formal parameter
                            names only give you names to refer more easily to the actual arguments.

                            So, you can use
                            function(c) { ... }
                            or even
                            function() {
                            var c = arguments[0];
                            ...
                            }

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            Working...