in textbox only number should enter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranesmitas
    New Member
    • Sep 2007
    • 21

    in textbox only number should enter

    In textbox i want to enter only number not any alphabates. please tell how i code that
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    what textboxes are you talking about? is it a webpage?

    kind regards

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      ... and what language?

      kind regards,

      Jos

      Comment

      • ranesmitas
        New Member
        • Sep 2007
        • 21

        #4
        In visual basic i put a textbox and i want to enter in that textbox only numeric value alphabete are not allowed .please how can i do it

        Comment

        • ranesmitas
          New Member
          • Sep 2007
          • 21

          #5
          Originally posted by gits
          hi ...

          what textboxes are you talking about? is it a webpage?

          kind regards

          In visual basic i put a textbox and i want to enter in that textbox only numeric value alphabete are not allowed .please how can i do it

          Comment

          • ranesmitas
            New Member
            • Sep 2007
            • 21

            #6
            Originally posted by JosAH
            ... and what language?

            kind regards,

            Jos
            In visual basic i put a textbox and i want to enter in that textbox only numeric value alphabete are not allowed .please how can i do it

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by ranesmitas
              In visual basic i put a textbox and i want to enter in that textbox only numeric value alphabete are not allowed .please how can i do it
              Moved to VB forum

              Comment

              • Robbie
                New Member
                • Mar 2007
                • 180

                #8
                Change Text1 to the name of your TextBox:

                [code=vb]
                Private Sub Text1_Change()
                Dim CurrentPos As Long
                Dim CurrentChar As String

                CurrentPos = 1
                While CurrentPos <= Len(Text1.Text)
                CurrentChar = Mid(Text1.Text, CurrentPos, 1)

                If Not (Asc(CurrentCha r) >= 48 And Asc(CurrentChar ) <= 57) Then
                Text1.Text = Replace(Text1.T ext, CurrentChar, "")
                End If

                CurrentPos = CurrentPos + 1
                Wend

                Text1.SelStart = Len(Text1.Text)
                End Sub
                [/code]

                If you want to also be able to enter a decimal point then change this one line:
                If Not (Asc(CurrentCha r) >= 48 And Asc(CurrentChar ) <= 57) Then
                into this one line:
                [code=vb]
                If Not ((Asc(CurrentCh ar) >= 48 And Asc(CurrentChar ) <= 57) or currentchar="." ) Then
                [/code]

                Comment

                • ranesmitas
                  New Member
                  • Sep 2007
                  • 21

                  #9
                  thank u for code but it gives error that

                  Run time error 5:
                  Invalid proceduer call or argument

                  this line
                  If Not (Asc(CurrentCha r) >= 48 And Asc(CurrentChar ) <= 57) Then

                  why it is

                  Comment

                  • Robbie
                    New Member
                    • Mar 2007
                    • 180

                    #10
                    Originally posted by ranesmitas
                    Run time error 5:
                    Invalid proceduer call or argument

                    this line
                    If Not (Asc(CurrentCha r) >= 48 And Asc(CurrentChar ) <= 57) Then
                    You must not be using VB6 then.
                    Seems the version you're using doesn't have the Asc() function.
                    If anyone used to .NET knows its equivalent to Asc(), it would be nice to hear from them now. =P

                    Comment

                    • QVeen72
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1445

                      #11
                      Originally posted by ranesmitas
                      In visual basic i put a textbox and i want to enter in that textbox only numeric value alphabete are not allowed .please how can i do it

                      Hi,

                      Write this in KeyPress Event of TextBox:

                      [code=vb]
                      If keyAscii <> 8 Then
                      If Instr("01234567 89", Chr(KeyAscii)) = 0 Then KeyAscii = 0
                      End If
                      [/code]

                      REgards
                      Veena

                      Comment

                      • hariharanmca
                        Top Contributor
                        • Dec 2006
                        • 1977

                        #12
                        Originally posted by QVeen72
                        Hi,
                        [code=vb]
                        If keyAscii <> 8 Then
                        If Instr("01234567 89", Chr(KeyAscii)) = 0 Then KeyAscii = 0
                        End If
                        [/code]
                        If keyAscii <> 8 Then
                        REgards
                        Veena
                        I think, you have to allow Ascii 13 also.

                        Comment

                        • QVeen72
                          Recognized Expert Top Contributor
                          • Oct 2006
                          • 1445

                          #13
                          Originally posted by hariharanmca
                          I think, you have to allow Ascii 13 also.
                          Ascii=13 = Enter key,
                          If She wants to write code to move to next Control for enter key, then she has to check and write code for that..
                          BCoz, VB will not move to next control for Enter key by itself

                          Regards
                          Veena

                          Comment

                          • hariharanmca
                            Top Contributor
                            • Dec 2006
                            • 1977

                            #14
                            Originally posted by QVeen72
                            Ascii=13 = Enter key,
                            If She wants to write code to move to next Control for enter key, then she has to check and write code for that..
                            BCoz, VB will not move to next control for Enter key by itself

                            Regards
                            Veena
                            Just think if she want to write code for any operation by keypress 13 then the above code change the Ascii to 0.
                            better allow Ascii 13.

                            Comment

                            • QVeen72
                              Recognized Expert Top Contributor
                              • Oct 2006
                              • 1445

                              #15
                              Hi Hari,

                              Yes, thats what i wrote in last post, if she want to write any code, she has to check that Ascii code and Exit Sub. Write my code in the End,
                              She may want to write for other chars also, SpaceBar, . - / ..

                              REgards
                              Veena

                              Comment

                              Working...