943,717 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 35272
  • VB.NET RSS
Jun 2nd, 2008
0

Textbox Validation

Expand Post »
Hi i want to prevent the user from entering number and special charaters in a textbox and another textbox from entering alphabets and specials characters .....

Need it quickly coz 2moro have to submit my project...
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
geetajlo is offline Offline
72 posts
since Sep 2007
Jun 2nd, 2008
5

Re: Textbox Validation

This following code just allowed you to entered numbers only (No alphabetics or any special characters) :
vb.net Syntax (Toggle Plain Text)
  1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
  3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
  4. e.Handled = True
  5. End If
  6. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
  7. e.Handled = False
  8. End If
  9. End Sub

This following code just allowed you to entered strings / alphabetics only (no numbers or any special characters):
vb.net Syntax (Toggle Plain Text)
  1. Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
  2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
  3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
  4. And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
  5. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
  6. 'Allowed space
  7. If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
  8. e.Handled = True
  9. End If
  10. End If
  11. ' Allowed backspace
  12. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
  13. e.Handled = False
  14. End If
  15. End Sub
Last edited by Jx_Man; Jun 2nd, 2008 at 8:33 am.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Jun 3rd, 2008
0

Re: Textbox Validation

To make text box to accept only numbers, in key press event of that textbox u can code like this


If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
MsgBox("Please enter valid number ")
End If


i think u can mannage other one..
Last edited by Pgmer; Jun 3rd, 2008 at 3:24 am.
Reputation Points: 51
Solved Threads: 112
Practically a Master Poster
Pgmer is offline Offline
668 posts
since Apr 2008
Mar 2nd, 2009
0

Re: Textbox Validation

Click to Expand / Collapse  Quote originally posted by geetajlo ...
Hi i want to prevent the user from entering number and special charaters in a textbox and another textbox from entering alphabets and specials characters .....

Need it quickly coz 2moro have to submit my project...
--------
Use the following code..
#Region "TextBox Events"

Private Sub txtFinRefNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFinRefNo.KeyPress
Try
If Char.IsLetterOrDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
Catch ex As Exception
ShowException(ex.Message, MESSAGEBOX_TITLE, ex)
End Try
End Sub
#End Region

sabeer pasha.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sabeerpasha is offline Offline
3 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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:
Previous Thread in VB.NET Forum Timeline: VB.NET and Excel
Next Thread in VB.NET Forum Timeline: Insert from DataGridView





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


Follow us on Twitter


© 2011 DaniWeb® LLC