Validate text with space

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

    #1

    Validate text with space



    hi to all!
    I just want to ask if how could i validate a text or string if it has a
    space between characters?

    Thanks in advance.

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

    #2
    Re: Validate text with space

    if (myString.index Of(' ') != -1) {
    /* your code */
    }


    Comment

    • Michael Winter

      #3
      Re: Validate text with space

      On Sat, 18 Dec 2004 12:19:08 +0100, VK <schools_ring@y ahoo.com> wrote:

      Please include quotes from the previous post.
      [color=blue]
      > if (myString.index Of(' ') != -1) {
      > /* your code */
      > }[/color]

      If the OP is trying to check if a space is between characters wouldn't the
      index need to be one or greater?

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • ms_chika

        #4
        Re: Validate text with space



        wow, thanks a lot!

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

        Comment

        • Randy Webb

          #5
          Re: Validate text with space

          Michael Winter wrote:
          [color=blue]
          > On Sat, 18 Dec 2004 12:19:08 +0100, VK <schools_ring@y ahoo.com> wrote:
          >
          > Please include quotes from the previous post.
          >[color=green]
          >> if (myString.index Of(' ') != -1) {
          >> /* your code */
          >> }[/color]
          >
          >
          > If the OP is trying to check if a space is between characters wouldn't
          > the index need to be one or greater?[/color]

          No. !1 = -1 means it was found in the string. But that doesn't satisfy
          the OP's requirements:

          ' myStringWithOnl yALeadingSpace'

          Passes that test but has no space "between" characters.

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

          Comment

          • Michael Winter

            #6
            Re: Validate text with space

            On Sat, 18 Dec 2004 14:40:24 -0500, Randy Webb <HikksNotAtHome @aol.com>
            wrote:

            [snip]
            [color=blue]
            > !1 = -1 means it was found in the string.[/color]

            Precisely. If the index was greater-than or equal to one, the space would
            have to occur after the first character.

            [snip]
            [color=blue]
            > Passes that test but has no space "between" characters.[/color]

            The best solution would be a regular expression.

            /^c+ c+$/ - Two compulsory words separated by a space.

            /^c+( c+)?$/ - Two words separated by a space. Second word is
            optional.

            /^c+( c+)*$/ - Two or more words separated by a space. Only first
            word is compulsory.

            /^c+( c+)+$/ - Two or more words separated by a space. First two
            words are compulsory.

            In each case, c should be replaced by a character class or escape, such as
            \w or [a-z]. You'd then use:

            if(/^\w+( \w+)?$/.test(str)) {
            /* String, str, passed */
            }

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • VK

              #7
              Re: Validate text with space

              Oh come on, guys...

              The OP said (adjusting the grammar) : "how to find if a string has at least
              one space between its characters"

              if (myString.index Of(' ') != -1) {
              /* your code */
              }
              gives the right answer: yes, it does.

              There are many other ways of course (escape, RegExp etc) but this one was
              the most obvious.



              Comment

              • Randy Webb

                #8
                Re: Validate text with space

                VK wrote:[color=blue]
                > Oh come on, guys...
                >
                > The OP said (adjusting the grammar) : "how to find if a string has at least
                > one space between its characters"
                >
                > if (myString.index Of(' ') != -1) {
                > /* your code */
                > }
                > gives the right answer: yes, it does.[/color]

                No, it doesn't give the right answer. Test the following two strings:

                myString = " String1"

                myString = "String2 "

                Both have spaces, both will pass the test, but neither has "at least one
                space *between* characters".

                A third, more obvious, example of failure is this:

                myString = " "

                What characters is that space between?

                <--snip-->

                And please, in the future, quote what you are replying to.


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

                Comment

                Working...