11 Replies - 26456 Views - Last Post: 04 January 2013 - 09:50 PM Rate Topic: -----

#1 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

textbox validation

Post icon  Posted 17 June 2008 - 10:54 AM

I am trying to validate user input into a textbox to make sure that the user has entered a number that is in a decimal format. (i.e. 15.1; 5.1 etc.) I think that I am close on the code below in terms of validating that the textbox input is a number but I am not sure how to validate the decimal. All suggestions are welcome. Thanks!



Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 
		 
		e.Handled = True		  
		If   e.KeyChar = ??? Or _ 
		   IsNumeric(e.KeyChar) Then _ 

		e.Handled = False 
End Sub 




Is This A Good Question/Topic? 1

Replies To: textbox validation

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9278
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: textbox validation

Posted 17 June 2008 - 11:25 AM

You do understand that Microsoft.VisualBasic.IsNumeric does this for you right?

(sorry, should clarify I am giving examples of what is numeric and not numeric with the 'isNumeric' function.. I did not mean for it be interpreted as 'select case' statement.)
case: 1
numeric
case: 1.0
numeric
case: 1.0.1
NOT numeric
case: a
not numeric
case: a.0
not numeric



Any particular reason why you are validating each character? Validate on the 'leave' event and if it is not numeric set the text box focus and then select all the text in the textbox

This post has been edited by modi123_1: 17 June 2008 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: textbox validation

Posted 17 June 2008 - 11:30 AM

You just need to compare it to the period character.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		
		e.Handled = True		  
		If   e.KeyChar = "." Or _
		   IsNumeric(e.KeyChar) Then _

		e.Handled = False
End Sub 

Was This Post Helpful? 1
  • +
  • -

#4 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: textbox validation

Posted 17 June 2008 - 11:32 AM

Thanks Modi. I had not considered plugging in Case or validating on the leave event. I will give that a shot.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9278
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: textbox validation

Posted 17 June 2008 - 11:51 AM

View Postgram999, on 17 Jun, 2008 - 01:32 PM, said:

Thanks Modi. I had not considered plugging in Case or validating on the leave event. I will give that a shot.


Woah there.. no I should have clarified.. by using the microsoft.vb.isnumeric you get what validation I think you are looking for.. the cases were just examples of what is considered numeric or not numeric with isNumeric.

Yes, but do try the leave event.
Was This Post Helpful? 1
  • +
  • -

#6 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: textbox validation

Posted 17 June 2008 - 12:29 PM

Thanks Jayman. Thanks for the clarification Modi. I found out pretty quickly that leave was not the way to go with this. And to answer your question; no, there is not reason to validate on each character all I am trying to do is validate format. I used to be able to validate format pretty easily in VB using something like:


textbox1.value = Format(textbox1.value, "#.#0; (#.#0)")




Unfortunately, it seems that VB.net is a bit more involved. I will keep playing with it. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: textbox validation

Posted 17 June 2008 - 09:23 PM

You can still format it in the manner you like, you will now use String.Format to do the formatting.

See if that is more in tune with what you were looking for.

String.Format

If you need to do a custom string format, then see Custom Numeric Format Strings
Was This Post Helpful? 1
  • +
  • -

#8 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: textbox validation

Posted 18 June 2008 - 07:32 AM

Thanks again Jayman! That is exactly what I was looking for. Have a good one!
Was This Post Helpful? 0
  • +
  • -

#9 bhuvamehul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-April 10

Re: textbox validation

Posted 07 April 2010 - 12:16 AM

This is the code for taking only decimal values in textbox



 Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       
            If (Asc(e.KeyChar) > 47 And Asc(e.KeyChar) < 58) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
                e.Handled = False
            Else
                e.Handled = True
            End If
       
       
    End Sub

'or you can try this also for taking only one '.'(dot) in textbox in whole string in key press event of textbox1
  If TextBox1.Text <> "" And TextBox1.Text.Contains(".") <> True Then
            If (Asc(e.KeyChar) > 47 And Asc(e.KeyChar) < 58) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        Else
            If (Asc(e.KeyChar) > 47 And Asc(e.KeyChar) < 58) Or Asc(e.KeyChar) = 8 Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        End If



[/quote]

This post has been edited by bhuvamehul: 07 April 2010 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

#10 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: textbox validation

Posted 07 April 2010 - 01:08 AM

Hi,

@modi123_1, IsNumeric function is legacy code, you shoud rather use Decimal.TryParse()...

Any code in the Microsoft.VisualBasic can be considered legacy.. (old stuff)
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9278
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: textbox validation

Posted 07 April 2010 - 07:49 AM

View Postmotcom, on 07 April 2010 - 02:08 AM, said:

Hi,

@modi123_1, IsNumeric function is legacy code, you shoud rather use Decimal.TryParse()...

Any code in the Microsoft.VisualBasic can be considered legacy.. (old stuff)

Check the post date bub... "Posted 17 June 2008 - 12:51 PM".

I hope you aren't going to go through all the older posts and explain their functions and methods two years ago are going to be depreciated... ;)

Side note, when and where is it explained that anything in "Microsoft.VisualBasic" or "isNumeric" is legacy? I flipped through MSDN and they don't have their usual message about it.

http://msdn.microsof...28VS.71%29.aspx

This post has been edited by modi123_1: 07 April 2010 - 07:52 AM

Was This Post Helpful? 1
  • +
  • -

#12 rakesh vish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-January 13

Re: textbox validation

Posted 04 January 2013 - 09:50 PM

You can try following code to validate interger with two decimal digits after decimal :

Dim i As Integer = Convert.ToInt32(e.KeyChar)
            If i = 46 Then
                If InStr(1, txt.Text, ".") > 0 Then
                    i = 0
                    MessageBox.Show("Multiple decimal points are not allowed.", "TextBox Validation")
                    e.Handled = True
                End If
            End If

            Dim text = txt.Text
            If Char.IsControl(e.KeyChar) Then
                'Allow all control characters.

            ElseIf Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c Then
                Dim selectionstart = txt.Selectionstart
                Dim selectionLength = txt.SelectionLength

                text = text.Substring(0, selectionstart) & e.KeyChar & text.Substring(selectionstart + selectionLength)
   [b][/b]             If Integer.TryParse(text, New Integer) AndAlso text.Length > 0 Then
                    e.Handled = False

                ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
                    'Reject a real number with two many decimal places.
                    e.Handled = True
                End If
            Else
                'Reject all other characters.
                e.Handled = True
            End If

This post has been edited by AdamSpeight2008: 04 January 2013 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1