C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to validate textboxes in a simple way


Posted Date: 10 Jul 2006    Resource Type: Articles    Category: .NET Framework
Author: Sameer KumarMember Level: Bronze    
Rating: 1 out of 5Points: 5




Take two text boxes txt1 and txt2 and two buttons
add code to keypress event of txtboxes



Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt1.KeyPress
If (Char.IsControl(e.KeyChar) = False) Then
If (Char.IsLetter(e.KeyChar)) Or (Char.IsWhiteSpace(e.KeyChar)) Then
'do nothing


Else
e.Handled = True
MsgBox("Sorry Only Character & Spaces Allowed!!", MsgBoxStyle.Information, "Verify")
txt1.Focus()



End If
End If


End Sub



Private Sub txt2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt2.KeyPress

If (Char.IsControl(e.KeyChar) = False) Then
If (Char.IsDigit(e.KeyChar)) Then
'do nothing


Else
e.Handled = True
MsgBox("Sorry Only Digits Allowed!!", MsgBoxStyle.Information, "Verify")
txt2.Focus()

End If
End If

End Sub




Also to ensure user must enter something and not blank spaces
put this code


Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

If (txt1.Text.Trim = "") Then
MsgBox("Blank not Allowed", MsgBoxStyle.Information, "Verify")
Else

MsgBox(txt1.Text, MsgBoxStyle.Information, "Verify")
End If
txt1.Clear()
txt1.Focus()


End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click

If (txt2.Text.Trim = "") Then
MsgBox("Blank not Allowedt", MsgBoxStyle.Information, "Verify")
Else

MsgBox(txt2.Text, MsgBoxStyle.Information, "Verify")
End If



txt2.Clear()
txt2.Focus()
End Sub
End Class



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Stored Procedure in Oracle
Previous Resource: Difference among Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use