TinyURL is a URL shortening service a web service that provides short URLs for redirection of long URLs. This can be achieved very easily in PHP, I posted a PHP snippet about this you could reach it from here. Last year I created a TinyURL shortener I named it TinyURL Desktop App you can download it from here if you want. I found the source code of TinyURL Desktop App just now that's why I am writing this hehe. I used Devcomponents in my TinyURL Desktop App also I didn't handle exceptions because I was a noob then :0.
Home » Posts filed under assign
How To Validate Textbox : Only Numbers, Only Characters, Not Null, Only Email in VB.NET
in
assign,
textbox,
tutorial,
validate email,
validation,
vb.net
- By
Mohamed Shimran
- on 4/09/2013
- 6 comments
Today I have brought you something delicious :) so you guys wonder how it works , first we have to create a module and we just have to assign the textbox for a validation (Only Number,Only Characters,Not Null,Only Email) so first of all lets create a new module and add this codes
Imports System.Text.RegularExpressions Module Module1 Public Enum ValidationType Only_Numbers = 1 Only_Characters = 2 Not_Null = 3 Only_Email = 4 End Enum Public Sub AssignValidation(ByRef CTRL As Windows.Forms.TextBox, ByVal Validation_Type As ValidationType) Dim txt As Windows.Forms.TextBox = CTRL Select Case Validation_Type Case ValidationType.Only_Numbers AddHandler txt.KeyPress, AddressOf number_Leave Case ValidationType.Only_Characters AddHandler txt.KeyPress, AddressOf OCHAR_Leave Case ValidationType.Not_Null AddHandler txt.Leave, AddressOf NotNull_Leave Case ValidationType.Only_Email AddHandler txt.Leave, AddressOf Email_Leave End Select End Sub Public Sub number_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Dim numbers As Windows.Forms.TextBox = sender If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then e.KeyChar = Chr(0) e.Handled = True End If End Sub Public Sub OCHAR_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) If InStr("1234567890!@#$%^&*()_+=-", e.KeyChar) > 0 Then e.KeyChar = Chr(0) e.Handled = True End If End Sub Public Sub NotNull_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Dim No As Windows.Forms.TextBox = sender If No.Text.Trim = "" Then MsgBox("This field Must be filled!") No.Focus() End If End Sub Public Sub Email_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Dim Email As Windows.Forms.TextBox = sender If Email.Text <> "" Then Dim rex As Match = Regex.Match(Trim(Email.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase) If rex.Success = False Then MessageBox.Show("Please Enter a valid Email Address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) Email.BackColor = Color.Red Email.Focus() Exit Sub Else Email.BackColor = Color.White End If End If End Sub End Module
now lets test the module i mean lets add 4 textboxes and assign each for a validation and see whether it works !
add 4 textboxes now add labels for each like this
you can only enter numbers in textbox1
you can only enter characters in textbox2
you cannot leave textbox3 blank
you can only enter email in textbox4
now go to form_load event and add these codes
AssignValidation(Me.TextBox1, ValidationType.Only_Numbers) AssignValidation(Me.TextBox2, ValidationType.Only_Characters) AssignValidation(Me.TextBox3, ValidationType.Not_Null) AssignValidation(Me.TextBox4, ValidationType.Only_Email)
thats where we assign the textbox for a validation !!
now lets test it just debug and try entering characters in textbox1 you cannot enter , go to textbox2 try entering characters you cannot enter , go to textbox3 and without entering anything go to textbox4 a message box will popup now go to textbox4 and enter something and click into another textbox and he textbox4 will be red in color and you will get a message
thanks for reading hope this is useful ! share ! comment !