How do I change this code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • effendi@epitome.com.sg

    #1

    How do I change this code

    A gentleman in this forum gave me this code and it work great to test
    that a string consist of only digits and it is 8 character long.

    (!/^\d{8}$/.test(ContactNo ) )

    How do I modify the above if I want the string to be of any length but
    consist of only digits?

    Thanks

  • Ivo

    #2
    Re: How do I change this code

    <effendi@epitom e.com.sg> wrote[color=blue]
    > A gentleman in this forum gave me this code and it work great to test
    > that a string consist of only digits and it is 8 character long.
    >
    > (!/^\d{8}$/.test(ContactNo ) )
    >
    > How do I modify the above if I want the string to be of any length but
    > consist of only digits?[/color]

    Change the three characters "{8}" into "{1,}" or into a single "+" sign.

    Or divide by 2 and see if it 's still a number...

    Or...
    --
    Ivo


    Comment

    • effendi@epitome.com.sg

      #3
      Re: How do I change this code

      Thanks, the code worked.

      I was just wondering, if you could explain how the syntax of the test
      is actually constructed? I would be highly appreciative.

      Regards

      Comment

      • Mick White

        #4
        Re: How do I change this code

        effendi@epitome .com.sg wrote:[color=blue]
        > Thanks, the code worked.
        >
        > I was just wondering, if you could explain how the syntax of the test
        > is actually constructed? I would be highly appreciative.
        >
        > Regards
        >[/color]
        Please quote the prevous posting...


        [Regular Expression Here].test("String Here")
        returns true or false


        alert(/^\d+$/.test("123")) //true
        alert(/^\d+$/.test("123a")) //false

        if(/^\d+$/.test(yourValue )){
        //good input, do stuff with it
        } else{
        //bad input
        }
        Mick

        Comment

        • RobB

          #5
          Re: How do I change this code

          effendi@epitome .com.sg wrote:[color=blue]
          > Thanks, the code worked.
          >
          > I was just wondering, if you could explain how the syntax of the test
          > is actually constructed? I would be highly appreciative.
          >
          > Regards[/color]

          Apologies to Ivo...

          Regular expression:

          / ---> delimeter (like quotes for a string, marks start & end)
          ^ ---> pattern must start at very beginning of sample string
          \d ---> metacharacter, matches one digit (0-9)
          + ---> quantifier, specifies one or more of the preceding
          (and replaces the {8}, which specifies exactly eight;
          {3,8}, e.g., would require between 3 and 8; {2,} requires at least two
          or more; '*' signifies zero or more, '?' is zero or one (i.e.,
          optional)
          $ ---> pattern must match to the end of sample string
          / ---> closing delimeter

          So: this will match a string that contains one or more digits. And
          nothing else. The exclamation mark before the call to test() 'flips'
          the result: if the test fails, false becomes true, which triggers the
          error message, etc.

          http://www.webreference.com/js/column5/index.html

          Comment

          • RobB

            #6
            Re: How do I change this code

            ....yikes.... #:-o

            * delimiter *

            Comment

            • Mick White

              #7
              Re: How do I change this code

              RobB wrote:
              [color=blue]
              > ...yikes.... #:-o
              >
              > * delimiter *
              >[/color]

              delimeter: Measures popularity of sandwich shops...
              Mick

              Comment

              • RobB

                #8
                Re: How do I change this code

                Mick White wrote:[color=blue]
                > RobB wrote:
                >[color=green]
                > > ...yikes.... #:-o
                > >
                > > * delimiter *
                > >[/color]
                >
                > delimeter: Measures popularity of sandwich shops...
                > Mick[/color]

                No, no, no...weighs the pastrami, bro' ! #:-D

                Comment

                • Effendi Baba

                  #9
                  Re: How do I change this code

                  Rob

                  Thank you for the explanation. It does seem that this test is only to
                  determine that a string contains only numbers. Can I use this test to
                  make sure that a string contain alphabets and numbers with no special
                  character?


                  Regular expression:
                  [color=blue]
                  >/ ---> delimeter (like quotes for a string, marks start & end)
                  >^ ---> pattern must start at very beginning of sample string
                  >\d ---> metacharacter, matches one digit (0-9)
                  >+ ---> quantifier, specifies one or more of the preceding
                  >(and replaces the {8}, which specifies exactly eight;
                  >{3,8}, e.g., would require between 3 and 8; {2,} requires >at least two
                  >or more; '*' signifies zero or more, '?' is zero or one
                  >(i.e., optional)[/color]



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

                  Comment

                  • Got Scripting?

                    #10
                    Re: How do I change this code

                    > Thank you for the explanation. It does seem that this test is only to[color=blue]
                    > determine that a string contains only numbers. Can I use this test to
                    > make sure that a string contain alphabets and numbers with no special
                    > character?[/color]

                    You can use "a test" (not "this test"), a "Regular Expression" test, to
                    do what you want, and more. All you have to do is learn about "Regular
                    Expressions". You've been pointed in the right direction. Now you can
                    google for tutorials on the topic.

                    Comment

                    • RobG

                      #11
                      Re: How do I change this code

                      Effendi Baba wrote:[color=blue]
                      > Rob
                      >
                      > Thank you for the explanation. It does seem that this test is only to
                      > determine that a string contains only numbers. Can I use this test to
                      > make sure that a string contain alphabets and numbers with no special
                      > character?
                      >[/color]

                      Please don't top-post.

                      Play with this:

                      <input type="text" onblur="checkMe (this.value);">

                      <script type="text/javascript">
                      function checkMe(x){
                      alert(!/\W+/.test(x));
                      }
                      </script>


                      \W matches any non-word character (i.e. not a-z A-Z or 0-9).
                      Putting ! in front negates the test, so if it finds one or more
                      non-word characters, it returns false.

                      Here is a reference here to help you learn RegExp:

                      <URL:http://www.webreferenc e.com/js/column5/>

                      --
                      Rob

                      Comment

                      • RobB

                        #12
                        Re: How do I change this code

                        Effendi Baba wrote:[color=blue]
                        > Rob
                        >
                        > Thank you for the explanation. It does seem that this test is only to
                        > determine that a string contains only numbers. Can I use this test to
                        > make sure that a string contain alphabets and numbers with no special
                        > character?
                        >
                        >
                        > Regular expression:
                        >[color=green]
                        > >/ ---> delimeter (like quotes for a string, marks start & end)
                        > >^ ---> pattern must start at very beginning of sample string
                        > >\d ---> metacharacter, matches one digit (0-9)
                        > >+ ---> quantifier, specifies one or more of the preceding
                        > >(and replaces the {8}, which specifies exactly eight;
                        > >{3,8}, e.g., would require between 3 and 8; {2,} requires >at least[/color][/color]
                        two[color=blue][color=green]
                        > >or more; '*' signifies zero or more, '?' is zero or one
                        > >(i.e., optional)[/color][/color]


                        if (/[^A-Z0-9]/i.test(ContactN o))
                        {...bad input...

                        [...] ---> character class, matches any one character in here
                        A-Z0-9 ---> matches A through Z, zero through nine
                        ^ ---> negates the above: match anything *but* A-Z, 0-9
                        /i ---> case-insensitive flag, so will (not) match a-z as well

                        No need to 'flip' the test result, as you're now testing for *invalid*
                        input...

                        There's a 'word' metacharacter as well (\w) - and its inverse (\W) for
                        'anything but a word character' - but they both include the underscore
                        as well, so the character class is necessary here.

                        Comment

                        Working...