Test for digit, regular expression

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

    Test for digit, regular expression

    Hello -

    I have a form field the results from which should not start with a
    digit. I have tried various permutations of the following without
    success. Can anyone help me out?

    //does not return false when it should
    (document.my_fo rm.my_field.val ue.substring(0) == / \d /)

    TIA

    Dave Miller
  • Evertjan.

    #2
    Re: Test for digit, regular expression

    Dave Miller wrote on 17 okt 2003 in comp.lang.javas cript:
    [color=blue]
    > I have a form field the results from which should not start with a
    > digit. I have tried various permutations of the following without
    > success. Can anyone help me out?
    >
    > //does not return false when it should
    > (document.my_fo rm.my_field.val ue.substring(0) == / \d /)[/color]

    It should NOT. Please read up on Reg Ex.

    =============== ========

    If (/\d/.test(document. my_form.my_fiel d.value.substri ng(0)))

    This will be true if the string contains at least 1 digit.

    =============== =========

    btw:

    document.my_for m.my_field.valu e.substring(0)

    and

    document.my_for m.my_field.valu e

    are equivalent

    =============== =========

    Not tested.

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

    Comment

    • Ivo

      #3
      Re: Test for digit, regular expression

      "Dave Miller" <unknown@unknow n.com> wrote in message
      news:MPG.19fa01 7126c8c3959896a 7@news.pa.comca st.giganews.com ...[color=blue]
      > Hello -
      >
      > I have a form field the results from which should not start with a
      > digit. I have tried various permutations of the following without
      > success. Can anyone help me out?
      >
      > //does not return false when it should
      > (document.my_fo rm.my_field.val ue.substring(0) == / \d /)
      >
      > TIA
      >
      > Dave Miller[/color]

      Remove the spaces from the regex, so
      / \d /
      becomes
      /\d/
      That may give some very different results. Additionally,
      /^\d/
      will match any string that starts with a digit.

      substring(0) returns the whole string, so you might as well use
      document.my_for m.my_field.valu e
      instead of
      document.my_for m.my_field.valu e.substring(0)

      You probably meant charAt(0), which is the first and nothing but the first
      character, but using the ^ inside the regex should do the trick anyways in
      this case.
      Ivo


      Comment

      • Dave Miller

        #4
        Re: Test for digit, regular expression

        In article <MPG.19fa017126 c8c3959896a7@ne ws.pa.comcast.g iganews.com>,
        unknown@unknown .com says...


        Thank you both for your help.

        Comment

        • TrueBlue

          #5
          Re: Test for digit, regular expression

          why not using just a regular expression?
          Sort of:

          /^\d+/.test(document. formName.fieldN ame.value);

          if it returns true, it means there are lweading digits. That's the most
          practical way, uses the built in method test() which requires a reg expr as
          its leading object. The regexp in case flags the following:
          ^ start of line
          \d a digit
          + one or plus times



          "Dave Miller" <unknown@unknow n.com>[color=blue]
          >
          > I have a form field the results from which should not start with a
          > digit. I have tried various permutations of the following without
          > success. Can anyone help me out?
          >
          > //does not return false when it should
          > (document.my_fo rm.my_field.val ue.substring(0) == / \d /)[/color]


          Comment

          • Dr John Stockton

            #6
            Re: Test for digit, regular expression

            JRS: In article <MPG.19fa017126 c8c3959896a7@ne ws.pa.comcast.g iganews.co
            m>, seen in news:comp.lang. javascript, Dave Miller <unknown@unknow n.com>
            posted at Fri, 17 Oct 2003 14:39:52 :-[color=blue]
            >
            >I have a form field the results from which should not start with a
            >digit. I have tried various permutations of the following without
            >success. Can anyone help me out?
            >
            >//does not return false when it should
            >(document.my_f orm.my_field.va lue.substring(0 ) == / \d /)[/color]


            The field.value is a string. There are exactly three types of string :
            Starting with a digit
            Starting with a non-digit
            Not starting - i.e. = "" // do not forget this case

            To test string S :
            OK = !/^\d/.test(S) // does not start with digit
            OK = /^\D/.test(S) // starts with non-digit

            It seems likely that in practice you have other conditions on the entry;
            the first-character test should be combined with the others using a
            longer RegExp, e.g.

            OK = /^\D.{3,5}$/.test(S) // non-digit; length 4..6

            --
            © 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

            Working...