Names in forms

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

    #1

    Names in forms

    Hi there,

    How can I use a regular expression to check that a name in a form field
    begins with a capital letter?


  • Dietmar Meier

    #2
    Re: Names in forms

    Steve Darby wrote:
    [color=blue]
    > How can I use a regular expression to check that a name in a form
    > field begins with a capital letter?[/color]

    var beginsWithCap = /^[A-Z]/.test(Element.n ame);

    Since this does not cover "É", "Ä" etc., I would rather use

    var firstChar = Element.name.ch arAt(0),
    beginsWithCap = firstChar.toUpp erCase() == firstChar;

    ciao, dhgm

    Comment

    • Dr John Stockton

      #3
      Re: Names in forms

      JRS: In article <362b1tF4rt1ogU 1@individual.ne t>, dated Sat, 29 Jan
      2005 21:47:28, seen in news:comp.lang. javascript, Dietmar Meier
      <usereplytoinst ead@innoline-systemtechnik.d e> posted :[color=blue]
      >Steve Darby wrote:
      >[color=green]
      >> How can I use a regular expression to check that a name in a form
      >> field begins with a capital letter?[/color]
      >
      >var beginsWithCap = /^[A-Z]/.test(Element.n ame);
      >
      >Since this does not cover "É", "Ä" etc., I would rather use
      >
      >var firstChar = Element.name.ch arAt(0),
      > beginsWithCap = firstChar.toUpp erCase() == firstChar;[/color]

      The OP wrote "a name"; the field might contain something like "dietmar
      meier", in which case two checks would be needed.

      And he may need to be more careful than that; surnames include such as
      O'Brien, McCarthy, Chomondeley-Featherstonehau gh, and "proper" names
      include such as Pope Gregory XIII.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
      PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
      Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

      Comment

      • Dietmar Meier

        #4
        Re: Names in forms

        Dr John Stockton wrote:
        [color=blue]
        > The OP wrote "a name"; the field might contain something like "dietmar
        > meier", in which case two checks would be needed.[/color]

        If that's the case, I would suggest

        var s = Element.value,
        e = 0,
        valid = s.replace(
        /\b\S/g,
        function(a) {
        e |= a != a.toUpperCase() ;
        }
        ), !e;
        [color=blue]
        > And he may need to be more careful than that; surnames include such as
        > O'Brien, McCarthy, Chomondeley-Featherstonehau gh, and "proper" names
        > include such as Pope Gregory XIII.[/color]

        On the other hand, "Piet van der Falk", "Karl von und zum Stein", etc.
        should be considered as valid names.

        ciao, dhgm

        Comment

        • Mick White

          #5
          Re: Names in forms

          Dietmar Meier wrote:

          [snip][color=blue]
          >
          > var s = Element.value,
          > e = 0,
          > valid = s.replace(
          > /\b\S/g,
          > function(a) {
          > e |= a != a.toUpperCase() ;
          > }
          > ), !e;[/color]

          e |= a != a.toUpperCase() ;

          I've stared at this long enough, Dietmar, can you explain what's
          happening in this line of code? Specifically,"e |="

          Mick

          Comment

          • Grant Wagner

            #6
            Re: Names in forms

            "Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
            news:9bRLd.1741 9$8H2.6437@twis ter.nyroc.rr.co m...[color=blue]
            > Dietmar Meier wrote:
            >
            > [snip][color=green]
            >>
            >> var s = Element.value,
            >> e = 0,
            >> valid = s.replace(
            >> /\b\S/g,
            >> function(a) {
            >> e |= a != a.toUpperCase() ;
            >> }
            >> ), !e;[/color]
            >
            > e |= a != a.toUpperCase() ;
            >
            > I've stared at this long enough, Dietmar, can you explain what's
            > happening in this line of code? Specifically,"e |="
            >
            > Mick[/color]

            e = e | (a != a.toUpperCase() );

            So -e- gets set to 1 if -e- is true (or non-null, or a non-empty string,
            or a non-zero number, or not undefined) or -a- is not equal to -a-
            converted to uppercase.

            He then takes advantage of the fact that the comma operator returns the
            value of the last operand to set -valid- to the negated value of -e-.

            --
            Grant Wagner <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq


            Comment

            • Mick White

              #7
              Re: Names in forms

              Grant Wagner wrote:
              [snip][color=blue]
              >
              >
              > e = e | (a != a.toUpperCase() );
              >
              > So -e- gets set to 1 if -e- is true (or non-null, or a non-empty string,
              > or a non-zero number, or not undefined) or -a- is not equal to -a-
              > converted to uppercase.
              >
              > He then takes advantage of the fact that the comma operator returns the
              > value of the last operand to set -valid- to the negated value of -e-.
              >[/color]

              Thanks, Grant, I'll have to investigate the "|" operator
              ..
              Mick

              Comment

              • rh

                #8
                Re: Names in forms

                Grant Wagner wrote:[color=blue]
                > "Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
                > news:9bRLd.1741 9$8H2.6437@twis ter.nyroc.rr.co m...[color=green]
                > > Dietmar Meier wrote:
                > >
                > > [snip][color=darkred]
                > >>
                > >> var s = Element.value,
                > >> e = 0,
                > >> valid = s.replace(
                > >> /\b\S/g,
                > >> function(a) {
                > >> e |= a != a.toUpperCase() ;
                > >> }
                > >> ), !e;[/color]
                > >[/color][/color]

                <...>
                [color=blue]
                > He then takes advantage of the fact that the comma operator returns[/color]
                the[color=blue]
                > value of the last operand to set -valid- to the negated value of -e-.
                >[/color]

                Not as given, though -- that will produce a syntax error in the "var"
                statement, as "!e" is not an identifier. Bracketing the expression
                "(...)" assigned to "valid" should give the intended effect that you
                describe.


                ../rh

                Comment

                • Dietmar Meier

                  #9
                  Re: Names in forms

                  rh wrote:
                  [color=blue]
                  > Not as given, though -- that will produce a syntax error in the "var"
                  > statement, as "!e" is not an identifier. Bracketing the expression
                  > "(...)" assigned to "valid" should give the intended effect that you
                  > describe.[/color]

                  Yes, absolutely correct. Disadvantage of copy&pray: Took it from
                  another piece of code where I had declared "valid" some lines
                  before and then simply added the comma before "valid = ...".

                  Corrected:

                  var s = Element.value,
                  e = 0,
                  valid = (s.replace(
                  /\b\S/g,
                  function(a) {
                  e |= a != a.toUpperCase() ;
                  }
                  ), !e);


                  ciao, dhgm

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Names in forms

                    JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
                    Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
                    <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
                    >
                    >Thanks, Grant, I'll have to investigate the "|" operator[/color]

                    Pray tell me what should be added to
                    <URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !


                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                    Comment

                    • Mick White

                      #11
                      Re: Names in forms

                      Dr John Stockton wrote:
                      [color=blue]
                      > JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
                      > Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
                      > <mwhite13BOGUS@ rochester.rr.co m> posted :
                      >[color=green]
                      >>Thanks, Grant, I'll have to investigate the "|" operator[/color]
                      >
                      >
                      > Pray tell me what should be added to
                      > <URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !
                      >[/color]


                      Good stuff, John, the truth table looks like a reverse NOR gate to me
                      (only false if both inputs are false).

                      1|0 : 1
                      1|1 : 1
                      0|1 : 1
                      0|0 : 0


                      Thanks.
                      Mick

                      Comment

                      • Lee

                        #12
                        Re: Names in forms

                        Mick White said:[color=blue]
                        >
                        >Dr John Stockton wrote:
                        >[color=green]
                        >> JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
                        >> Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
                        >> <mwhite13BOGUS@ rochester.rr.co m> posted :
                        >>[color=darkred]
                        >>>Thanks, Grant, I'll have to investigate the "|" operator[/color]
                        >>
                        >>
                        >> Pray tell me what should be added to
                        >> <URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !
                        >>[/color]
                        >
                        >
                        >Good stuff, John, the truth table looks like a reverse NOR gate to me
                        >(only false if both inputs are false).
                        >
                        >1|0 : 1
                        >1|1 : 1
                        >0|1 : 1
                        >0|0 : 0[/color]

                        A "reverse NOR gate" being, of course, an OR gate.

                        Comment

                        • Mick White

                          #13
                          Re: Names in forms

                          Lee wrote:
                          [color=blue]
                          > Mick White said:[/color]
                          [color=blue][color=green]
                          >>
                          >>Good stuff, John, the truth table looks like a reverse NOR gate to me
                          >>(only false if both inputs are false).
                          >>
                          >>1|0 : 1
                          >>1|1 : 1
                          >>0|1 : 1
                          >>0|0 : 0[/color]
                          >
                          >
                          > A "reverse NOR gate" being, of course, an OR gate.
                          >[/color]

                          Yes of course!!!!
                          It's been a while since I have worked with SSE.
                          Mick

                          Comment

                          Working...