Little problem for me...about checking a textbox!

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

    #1

    Little problem for me...about checking a textbox!

    Hi, i'm italian...so... excuse me for my english.
    I've a little problem....in what manner i can check a textbox for know if it
    contain only character from A-Z (a-z), numbers (0-9), and underscore (-) and
    dot (.) ?!?!?!?

    I use vb.net.

    thank to all.


  • Joseph Bittman MCSD

    #2
    Re: Little problem for me...about checking a textbox!

    August 5, 2005

    Regular Expressions allow you to easily check whether text contains
    certain valid (or invalid) characters. It is located in the
    System.Text.Reg ularExpressions namespace:

    Imports System.Text.Reg ularExpressions

    dim valid as boolean = false

    dim expression as string = "[A-Za-z/d-.]{0,}"
    'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
    allowing unlimited numbers of those chars. Then use this to check whether it
    is true or not:

    valid = regex.IsMatch(I nputString,Expr ession) ' Checks whether the
    inputstring matches the rules in the expression. (the signature might have
    to be slightly modified because of my poor memory) Regex is a shared class.

    if valid = false then
    msgbox("Invalid input!!!")
    end if

    the /d in the regular expression should stand for 0-9; somebody correct me
    if I did this wrong!

    You might look at:

    http://msdn.microsoft.com/library/de...sOperators.asp

    for more info! Hope this helps and have a great day!

    --
    Joseph Bittman
    Microsoft Certified Solution Developer

    Web Site: http://71.39.42.23
    Static IP




    "tranky" <NOSPAM@THANKY. YOU> wrote in message
    news:d8OIe.3645 4$2U1.2048207@n ews3.tin.it...[color=blue]
    > Hi, i'm italian...so... excuse me for my english.
    > I've a little problem....in what manner i can check a textbox for know if
    > it
    > contain only character from A-Z (a-z), numbers (0-9), and underscore (-)
    > and dot (.) ?!?!?!?
    >
    > I use vb.net.
    >
    > thank to all.
    >[/color]


    Comment

    • 

      #3
      Re: Little problem for me...about checking a textbox!

      | the /d in the regular expression should stand for 0-9; somebody correct me
      | if I did this wrong!

      \d and \. were incorrect (just letters, numbers, and underscores...n o
      period).

      it may be worth mentioning in what event to place this code. you can either
      validate as the user types or after all the input has been provided and the
      textbox begins validation.

      to simplify the all of the above:

      private sub yourTextBoxEven t( _
      byval sender as object, _
      [args here for appropriate event]_
      ) _
      handles yourTextBox.[appropriate event])
      if not typeof sender is textbox then return
      dim textBox as textbox = sender
      dim exp As new regex("^[a-z\d_]*$", regexoptions.ig norecase)
      if exp.ismatch(tex tBox.text) then return
      msgbox("invalid input supplied")
      end sub

      hth,

      me


      Comment

      • Dragon

        #4
        Re: Little problem for me...about checking a textbox!

        > the /d in the regular expression should stand for 0-9; somebody correct me[color=blue]
        > if I did this wrong![/color]

        Correcting. 8=]

        0-9 is \d

        "[A-Za-z/d-.]{0,}" — it doesn't work!
        [color=blue]
        > 'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
        > allowing unlimited numbers of those chars.[/color]

        But, it would check if input ^contains^ valid string, but not if it's valid
        wholly.

        I offer the following:

        ~
        Dim rx As String = "[^\.\d\w_]"
        If System.Text.Reg ularExpressions .Regex.IsMatch( TextBox1.Text, rx) Then
        REM Hey! There's undesirable character!
        Else
        REM All right!
        End If
        ~

        Best regards,

        Roman


        Comment

        • 

          #5
          Re: Little problem for me...about checking a textbox!

          | "[A-Za-z/d-.]{0,}" — it doesn't work!

          no, it doesn't.

          "[^\.\d\w_]"

          nor does that.

          this *does* based on the op's requirements:

          "^[a-z\d_]*$"

          the requirements were:

          a-z
          A-Z
          0-9
          _

          why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
          string has to *start* with a *single correct character* but can be followed
          by *any* character.


          Comment

          • Some Guy

            #6
            Re: Little problem for me...about checking a textbox!

            Dragon,

            This "[^\.\d\w_]" will allow an empty string. Should be "[^\.\d\w_]*"

            "Dragon" <no@spam.please > wrote in message
            news:eGQwrWfmFH A.1372@TK2MSFTN GP10.phx.gbl...[color=blue][color=green]
            >> the /d in the regular expression should stand for 0-9; somebody correct
            >> me
            >> if I did this wrong![/color]
            >
            > Correcting. 8=]
            >
            > 0-9 is \d
            >
            > "[A-Za-z/d-.]{0,}" - it doesn't work!
            >[color=green]
            >> 'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
            >> allowing unlimited numbers of those chars.[/color]
            >
            > But, it would check if input ^contains^ valid string, but not if it's
            > valid
            > wholly.
            >
            > I offer the following:
            >
            > ~
            > Dim rx As String = "[^\.\d\w_]"
            > If System.Text.Reg ularExpressions .Regex.IsMatch( TextBox1.Text, rx) Then
            > REM Hey! There's undesirable character!
            > Else
            > REM All right!
            > End If
            > ~
            >
            > Best regards,
            >
            > Roman
            >
            >[/color]


            Comment

            • Some Guy

              #7
              Re: Little problem for me...about checking a textbox!

              > why are periods in both you guy's exps?

              Because he wanted to check for a period

              [color=blue]
              > also, "[^\.\d\w_]" only says the
              > string has to *start* with a *single correct character* but can be
              > followed
              > by *any* character.[/color]

              Did you try it?

              "" <a@b.com> wrote in message news:cEPIe.1182 $Mq4.995@fe05.l ga...[color=blue]
              >| "[A-Za-z/d-.]{0,}" - it doesn't work!
              >
              > no, it doesn't.
              >
              > "[^\.\d\w_]"
              >
              > nor does that.
              >
              > this *does* based on the op's requirements:
              >
              > "^[a-z\d_]*$"
              >
              > the requirements were:
              >
              > a-z
              > A-Z
              > 0-9
              > _
              >
              > why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
              > string has to *start* with a *single correct character* but can be
              > followed
              > by *any* character.
              >
              >[/color]


              Comment

              • 

                #8
                Re: Little problem for me...about checking a textbox!

                | Because he wanted to check for a period

                "he" not being the op. i didn't see that the op wanted a period. did i miss
                it?

                | > also, "[^\.\d\w_]" only says the
                | > string has to *start* with a *single correct character* but can be
                | > followed
                | > by *any* character.
                |
                | Did you try it?

                actually, i did not...and, i mis-read the exp and thought the ^ was outside
                the character class.


                Comment

                • 

                  #9
                  Re: Little problem for me...about checking a textbox!

                  you're right! the op *did* want to check for a period...had to go back and
                  look again. ;^)

                  i don't think i saw whether he said a blank string was also valid or if it
                  had to have some kind of valid input.


                  "Some Guy" <noplace@nowher e.com> wrote in message
                  news:uEOGs9fmFH A.2852@TK2MSFTN GP15.phx.gbl...
                  |> why are periods in both you guy's exps?
                  |
                  | Because he wanted to check for a period
                  |
                  |
                  | > also, "[^\.\d\w_]" only says the
                  | > string has to *start* with a *single correct character* but can be
                  | > followed
                  | > by *any* character.
                  |
                  | Did you try it?
                  |
                  | "" <a@b.com> wrote in message news:cEPIe.1182 $Mq4.995@fe05.l ga...
                  | >| "[A-Za-z/d-.]{0,}" - it doesn't work!
                  | >
                  | > no, it doesn't.
                  | >
                  | > "[^\.\d\w_]"
                  | >
                  | > nor does that.
                  | >
                  | > this *does* based on the op's requirements:
                  | >
                  | > "^[a-z\d_]*$"
                  | >
                  | > the requirements were:
                  | >
                  | > a-z
                  | > A-Z
                  | > 0-9
                  | > _
                  | >
                  | > why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
                  | > string has to *start* with a *single correct character* but can be
                  | > followed
                  | > by *any* character.
                  | >
                  | >
                  |
                  |


                  Comment

                  • Joseph Bittman MCSD

                    #10
                    Re: Little problem for me...about checking a textbox!

                    August 5, 2005

                    LOL Thanks for:

                    #1 the reminder that it is backslashes and not forwardslashes.
                    #2 that the period needs a backslash behind it

                    Now, I think there is something that we all missed.... (with the assumption
                    that blank strings are okay), is to append the ^ to the front AND and $ to
                    the back... which means that the WHOLE string must conform to the pattern
                    and not just one part (which can be just 1 char). This is a common security
                    slip up that even I made a mistake in my original post. Also, I always like
                    to list out things like A-Za-z so that I never accidentally forget that a
                    char set contains something I didn't want like a char return, etc. So:

                    if regex.ismatch(i nputstring,"^[A-Za-z\d\._]{0,}$") = false then
                    msgbox("Invalid ")
                    else
                    msgbox("Valid")
                    end if

                    This will work and I have tested it. Hope this helps and people are free to
                    comment!

                    --
                    Joseph Bittman
                    Microsoft Certified Solution Developer

                    Web Site: http://71.39.42.23
                    Static IP




                    "" <a@b.com> wrote in message news:cEPIe.1182 $Mq4.995@fe05.l ga...[color=blue]
                    >| "[A-Za-z/d-.]{0,}" - it doesn't work!
                    >
                    > no, it doesn't.
                    >
                    > "[^\.\d\w_]"
                    >
                    > nor does that.
                    >
                    > this *does* based on the op's requirements:
                    >
                    > "^[a-z\d_]*$"
                    >
                    > the requirements were:
                    >
                    > a-z
                    > A-Z
                    > 0-9
                    > _
                    >
                    > why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
                    > string has to *start* with a *single correct character* but can be
                    > followed
                    > by *any* character.
                    >
                    >[/color]


                    Comment

                    • Some Guy

                      #11
                      Re: Little problem for me...about checking a textbox!

                      < #2 that the period needs a backslash behind it

                      I think you mean in front of it don't you?


                      "Joseph Bittman MCSD" <RyanBittman@ms n.com> wrote in message
                      news:uuh0XhhmFH A.1148@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > August 5, 2005
                      >
                      > LOL Thanks for:
                      >
                      > #1 the reminder that it is backslashes and not forwardslashes.
                      > #2 that the period needs a backslash behind it
                      >
                      > Now, I think there is something that we all missed.... (with the
                      > assumption that blank strings are okay), is to append the ^ to the front
                      > AND and $ to the back... which means that the WHOLE string must conform to
                      > the pattern and not just one part (which can be just 1 char). This is a
                      > common security slip up that even I made a mistake in my original post.
                      > Also, I always like to list out things like A-Za-z so that I never
                      > accidentally forget that a char set contains something I didn't want like
                      > a char return, etc. So:
                      >
                      > if regex.ismatch(i nputstring,"^[A-Za-z\d\._]{0,}$") = false then
                      > msgbox("Invalid ")
                      > else
                      > msgbox("Valid")
                      > end if
                      >
                      > This will work and I have tested it. Hope this helps and people are free
                      > to comment!
                      >
                      > --
                      > Joseph Bittman
                      > Microsoft Certified Solution Developer
                      >
                      > Web Site: http://71.39.42.23
                      > Static IP
                      >
                      >
                      >
                      >
                      > "" <a@b.com> wrote in message news:cEPIe.1182 $Mq4.995@fe05.l ga...[color=green]
                      >>| "[A-Za-z/d-.]{0,}" - it doesn't work!
                      >>
                      >> no, it doesn't.
                      >>
                      >> "[^\.\d\w_]"
                      >>
                      >> nor does that.
                      >>
                      >> this *does* based on the op's requirements:
                      >>
                      >> "^[a-z\d_]*$"
                      >>
                      >> the requirements were:
                      >>
                      >> a-z
                      >> A-Z
                      >> 0-9
                      >> _
                      >>
                      >> why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
                      >> string has to *start* with a *single correct character* but can be
                      >> followed
                      >> by *any* character.
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Joseph Bittman MCSD

                        #12
                        Re: Little problem for me...about checking a textbox!

                        August 5, 2005

                        Depends on how you normally say "behind or in-front"... LOL I think you got
                        the point! :-)

                        --
                        Joseph Bittman
                        Microsoft Certified Solution Developer

                        Web Site: http://71.39.42.23
                        Static IP




                        "Some Guy" <noplace@nowher e.com> wrote in message
                        news:OEwE%23ohm FHA.1464@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                        >< #2 that the period needs a backslash behind it
                        >
                        > I think you mean in front of it don't you?
                        >
                        >
                        > "Joseph Bittman MCSD" <RyanBittman@ms n.com> wrote in message
                        > news:uuh0XhhmFH A.1148@TK2MSFTN GP12.phx.gbl...[color=green]
                        >> August 5, 2005
                        >>
                        >> LOL Thanks for:
                        >>
                        >> #1 the reminder that it is backslashes and not forwardslashes.
                        >> #2 that the period needs a backslash behind it
                        >>
                        >> Now, I think there is something that we all missed.... (with the
                        >> assumption that blank strings are okay), is to append the ^ to the front
                        >> AND and $ to the back... which means that the WHOLE string must conform
                        >> to the pattern and not just one part (which can be just 1 char). This is
                        >> a common security slip up that even I made a mistake in my original post.
                        >> Also, I always like to list out things like A-Za-z so that I never
                        >> accidentally forget that a char set contains something I didn't want like
                        >> a char return, etc. So:
                        >>
                        >> if regex.ismatch(i nputstring,"^[A-Za-z\d\._]{0,}$") = false then
                        >> msgbox("Invalid ")
                        >> else
                        >> msgbox("Valid")
                        >> end if
                        >>
                        >> This will work and I have tested it. Hope this helps and people are free
                        >> to comment!
                        >>
                        >> --
                        >> Joseph Bittman
                        >> Microsoft Certified Solution Developer
                        >>
                        >> Web Site: http://71.39.42.23
                        >> Static IP
                        >>
                        >>
                        >>
                        >>
                        >> "" <a@b.com> wrote in message news:cEPIe.1182 $Mq4.995@fe05.l ga...[color=darkred]
                        >>>| "[A-Za-z/d-.]{0,}" - it doesn't work!
                        >>>
                        >>> no, it doesn't.
                        >>>
                        >>> "[^\.\d\w_]"
                        >>>
                        >>> nor does that.
                        >>>
                        >>> this *does* based on the op's requirements:
                        >>>
                        >>> "^[a-z\d_]*$"
                        >>>
                        >>> the requirements were:
                        >>>
                        >>> a-z
                        >>> A-Z
                        >>> 0-9
                        >>> _
                        >>>
                        >>> why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
                        >>> string has to *start* with a *single correct character* but can be
                        >>> followed
                        >>> by *any* character.
                        >>>
                        >>>[/color]
                        >>
                        >>[/color]
                        >
                        >[/color]


                        Comment

                        • tranky

                          #13
                          Re: Little problem for me...about checking a textbox!

                          THANKS TO ALL!!!


                          Comment

                          • Dragon

                            #14
                            Re: Little problem for me...about checking a textbox!

                            > This "[^\.\d\w_]" will allow an empty string. Should be "[^\.\d\w_]*"

                            And this won't allow a valid string. Actually it matches any string.

                            Though, I missed some detail...
                            [color=blue]
                            > contain only character from A-Z (a-z)[/color]

                            \w matches any letter, so regex would be: "[^A-Za-z\d\._]".

                            I didn't see if tranky worried about blank strings, however if he did,
                            "[^A-Za-z\d\._]|^$" will do the job.



                            Comment

                            • Joseph Bittman MCSD

                              #15
                              Re: Little problem for me...about checking a textbox!

                              August 6, 2005

                              More than welcome! :-)

                              --
                              Joseph Bittman
                              Microsoft Certified Solution Developer

                              Web Site: http://71.39.42.23
                              Static IP




                              "tranky" <NOSPAM@THANKY. YOU> wrote in message
                              news:iWZIe.3715 9$2U1.2094475@n ews3.tin.it...[color=blue]
                              > THANKS TO ALL!!!
                              >[/color]


                              Comment

                              Working...