943,769 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3022
  • VB.NET RSS
Oct 3rd, 2008
0

validate user input

Expand Post »
Hi all,
I have different text boxes the I need to validate the user inut. The code that I have (example) does work, the problem is that if there is absolutly nothing in the text box I don't want to get the error message. If I have a starting "0", physically in the text box, or "non-numeric value" then I do want the error message. What happens is when I "clear" everything in the boxes then I get the error also but don't want it if nothing is in the boxes.

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub txtInterestRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInterestRate.TextChanged
  2. Try
  3.  
  4. If Me.txtInterestRate.Text.Length = 0 Then 'reads interest rate text box
  5. Me.errInput.SetError(Me.txtInterestRate, "Please Enter a valid value") 'if 0 gives error
  6.  
  7. ElseIf Not IsNumeric(txtInterestRate.Text) Then 'checks input for numeric value
  8. Me.errInput.SetError(Me.txtInterestRate, "Please Enter a valid Numeric value") 'if non-numerics gives error
  9. Throw New Exception("Please enter a valid numeric value")
  10.  
  11. ElseIf txtInterestRate.Text = 0 Then 'checks input for numeric value
  12. Me.errInput.SetError(Me.txtLoanAmount, "Please Enter a Non Zero valid Numeric value") 'if non-numerics gives error
  13. Throw New Exception("Please enter a valid Non Zero numeric value")
  14.  
  15. End If
  16.  
  17. Catch ex As Exception 'exception handeler for error input
  18. MsgBox(ex.Message) 'displays error
  19. Me.txtInterestRate.Text = ("")
  20. End Try
  21. End Sub
Any help would really be appreciated!
Thanks,
Ken
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 3rd, 2008
0

Re: validate user input

What about right after Try inserting

If Me.txtInterestRate.Text <> ""
/// do all of your stuff
End If
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Oct 4th, 2008
0

Re: validate user input

rapture,
Thank you very much for your reply. I added your line of code to all the text boxes and it worked great.
Thanks,
Ken
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 7th, 2008
0

Re: validate user input

Hi All,

I have been trying to figure out another problem with this program, maybe some one can help me out.
I need some type of code so if the text field is left completey empty it will give an error and tell the user to please input a number.
I just haven't been able to figure it out.

Help Please,
Ken
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Oct 7th, 2008
0

Re: validate user input

You may want to check that when user presses Ok-button (or any similar button):
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
  2. ' Check empty text
  3.  
  4. If Me.txtInterestRate.Text.Trim = "" Then
  5. MessageBox.Show("Please enter interest rate", _
  6. "Missing Information", _
  7. MessageBoxButtons.OK, _
  8. MessageBoxIcon.Warning)
  9. Me.txtInterestRate.Focus() ' Set focus to field if you want
  10. End If
  11.  
  12. End Sub
or "integrate" this with rapture's answer:
VB.NET Syntax (Toggle Plain Text)
  1. If Me.txtInterestRate.Text <> ""
  2. /// do all of your stuff
  3. Else
  4. MessageBox.Show("Please enter interest rate", _
  5. "Missing Information", _
  6. MessageBoxButtons.OK, _
  7. MessageBoxIcon.Warning)
  8. End If
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Oct 8th, 2008
0

Re: validate user input

Teme64,
Thank you for your input, it works great now. I just couldn't figure that one out.
Thanks,
tyserman5674
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC