Function to capitalise each word

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

    Function to capitalise each word

    Repost - as some readers took got sidetracked.

    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 addresses like "8 brown road, chatham, kent" and so I have to
    manually change them.


    --
    Nige

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

    #2
    Re: Function to capitalise each word

    Nige wrote on 11 nov 2003 in comp.lang.javas cript:
    [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.[/color]

    A better result is a capital after any nonalphabetical char
    [words like van, de in Dutch and l' in French, etc. need seperate coding]


    <script>

    function properCase(a) {
    var b="";
    var notyet=true;
    for (i=0;i<=a.lengt h;i++) {
    m = a.substr(i,1)
    b += (notyet)?m.toUp perCase():m.toL owerCase();
    notyet = (m.toUpperCase( ) == m.toLowerCase() )
    }
    return b
    }

    document.write( properCase("the bluE Fox O'bRian and hip-hap+hOP"));

    </script>

    This is analogous to my 2002 vbscript function in:

    <http://groups.google.com/
    groups?selm=Xns 921ECA520FFABee jj99%40194.109. 6.74>


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

    Comment

    • Dr John Stockton

      #3
      Re: Function to capitalise each word

      JRS: In article <k3d2rv8q5pib6e h2775076q9et3sp qt03l@4ax.com>, seen in
      news:comp.lang. javascript, Nige <uYYYY@ntlworld .com> posted at Tue, 11
      Nov 2003 19:13:54 :-[color=blue]
      >Repost - as some readers took got sidetracked.
      >
      >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 addresses like "8 brown road, chatham, kent" and so I have to
      >manually change them.[/color]

      This should, perhaps, be done with String.replace( RE, Fnc), using a
      RegExp to isolate each word and a function to UpCase its first
      character.

      Here's another way, partially satisfactory :

      S = "3 smith street"
      S = S.split(/\W+/)
      for (j=0; j<S.length;j++ ) S[j] =
      S[j].substring(0,1) .toUpperCase() + S[j].substring(1)
      S.join(' ')

      But how do you deal with Stoke-on-Trent or Bridge of Weir , let alone
      s'Hertogenbosch ?

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      • Evertjan.

        #4
        Re: Function to capitalise each word

        Dr John Stockton wrote on 11 nov 2003 in comp.lang.javas cript:
        [color=blue]
        > But how do you deal with Stoke-on-Trent or Bridge of Weir , let alone
        > s'Hertogenbosch ?
        >[/color]

        That "left alone" last one is easy:

        s = "S'hertoGenbosc h"
        s = s.replace(/s'Hertogenbosch/i,"Den Bosch")
        if (today=="carnav al")
        s = s.replace(/Den Bosch/,"Oeteldonk" )


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

        Comment

        • Nige

          #5
          Re: Function to capitalise each word

          In comp.lang.javas cript, Evertjan. wrote:
          [color=blue]
          >Nige wrote on 11 nov 2003 in comp.lang.javas cript:
          >[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.[/color]
          >
          >A better result is a capital after any nonalphabetical char
          >[words like van, de in Dutch and l' in French, etc. need seperate coding][/color]


          Thanks for your solution, I've now incorporated it into the form, but
          rather than changing the value, I use it to alert the user how the text
          should (probably) appear; a good compromise I think.


          --
          Nige

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

          Comment

          • Evertjan.

            #6
            Re: Function to capitalise each word

            Nige wrote on 26 nov 2003 in comp.lang.javas cript:
            [color=blue]
            > In comp.lang.javas cript, Evertjan. wrote:
            >[color=green]
            >>Nige wrote on 11 nov 2003 in comp.lang.javas cript:
            >>[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.[/color]
            >>
            >>A better result is a capital after any nonalphabetical char
            >>[words like van, de in Dutch and l' in French, etc. need seperate coding][/color]
            >
            >
            > Thanks for your solution, I've now incorporated it into the form, but
            > rather than changing the value, I use it to alert the user how the text
            > should (probably) appear; a good compromise I think.[/color]

            Show us the resulting URL
            WHEN you are ready and IF possible.

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

            Comment

            • Nige

              #7
              Re: Function to capitalise each word

              In comp.lang.javas cript, Evertjan. wrote:
              [color=blue]
              >Show us the resulting URL
              >WHEN you are ready and IF possible.[/color]

              OK, but it ain't pretty:







              --
              Broadband in the Weald of Kent

              Comment

              • Evertjan.

                #8
                Re: Function to capitalise each word

                Nige wrote on 26 nov 2003 in comp.lang.javas cript:
                [color=blue]
                > In comp.lang.javas cript, Evertjan. wrote:
                >[color=green]
                >>Show us the resulting URL
                >>WHEN you are ready and IF possible.[/color]
                >
                > OK, but it ain't pretty:
                >
                > http://www.wealdbroadband.co.uk/register.htm[/color]

                Not bad.

                Perhaps you could change to an

                newString=promp t("Capitalized\ nAccept, change or refuse",sugStri ng);


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

                Comment

                • Evertjan.

                  #9
                  Re: Function to capitalise each word

                  Nige wrote on 28 nov 2003 in comp.lang.javas cript:
                  [color=blue]
                  > In comp.lang.javas cript, Evertjan. wrote:
                  >[color=green]
                  >>newString=pro mpt("Capitalize d\nAccept, change or refuse",sugStri ng);[/color]
                  >
                  > OK, I've tried it, and it nearly works! I'm having a problem if the
                  > user clicks Cancel. This is the code I've used (apologies):
                  >
                  > newString=promp t("Capitalized\ nAccept, change or
                  > refuse",sugStri ng);
                  > if (sugString == null)
                  > return "TESTING - should return me"
                  > else return sugString[/color]

                  1
                  I suppose you want to test the newString, as the sugString is input.

                  2
                  the "else" is superfluous.

                  Try this:

                  <script>

                  function AskForCaps(sugS tring, origString){
                  newString=promp t("Capitalized\ n(change+)OK or cancel",sugStri ng);
                  if (newString == null)
                  return origString
                  return newString
                  }


                  alert(AskForCap s("John Doe", "john doe")) // test

                  </script>


                  [color=blue]
                  > Also, I quite liked the way that alert() makes a sound, and also
                  > positions the pop-up window close to the cursor, rather than at the
                  > edge of the main window. Can this be changed?[/color]

                  No idea.


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

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Function to capitalise each word

                    Nige wrote:[color=blue]
                    > Also, I quite liked the way that alert() makes a sound, and also
                    > positions the pop-up window close to the cursor, rather than at
                    > the edge of the main window. Can this be changed?[/color]

                    No, you describe features of a particular user agent (IE). My
                    Mozilla/5.0, e.g., although also running under Win2k,does neither make
                    the Windows alert sound (and *I* am thankful for that) nor does it
                    position such _dialogs_ (not really pop-up windows) near the cursor (it
                    positions it in the center of Navigator window instead). Also note that
                    a mouse device does not need to be present to navigate web documents, so
                    there does not need to be a mouse cursor that can be used for positioning.


                    PointedEars
                    --
                    apprentice.c - parses /etc/magic to learn magic

                    (from the file-3.40 README)

                    Comment

                    • Mark Szlazak

                      #11
                      Re: Function to capitalise each word

                      JavaScript The Definitive Guide - 4th Ed. pp 526, 527.

                      SYNOPSIS
                      string.replace( regexp, replacement)

                      DESCRIPTION
                      ECMAScript v3 specifies that the "replacemen t" argument to replace()
                      may be a function instead of a string, and this feature is implemented
                      in JavaScript 1.2 and JScript 5.5+. In this case, the function is
                      invoked for each match and the string it returns is used as the
                      replacement text. The first argument to the function is the string that
                      matched the pattern. The next arguments are the strings that matched
                      any parenthesized subexpressions within the pattern. There may be zero
                      or more of these arguments. The next argument is an integer that
                      specifies the position within "string" at which the match occurred, and
                      the final argument to the "replacemen t" function is the "string" itself.

                      EXAMPLE

                      To capitalize the first letter of all words in a string:

                      text.replace(/\b\w+\b/g, function(word) {
                      return word.substring( 0,1).toUpperCas e()
                      + word.substring( 1);
                      });




                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      Working...