Friday, June 27, 2008

URL validation (vb.net)

'URL valid in textbox..


Private Sub txtURL_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.Validated
If txtURL.Text.Trim() <> "" Then

Dim url As String = txtURL.Text.ToString.Trim()
Dim validURL As Boolean = UrlIsValid(url)
If validURL = False Then
MsgBox("Please, Enter Valid URL")
txtURL.Focus()
End If
End If
End Sub

Public Function UrlIsValid(ByVal url As String) As Boolean
If url.ToLower().StartsWith("www.") Then url = "http://" & url
Dim webResponse As Net.HttpWebResponse = Nothing
Try
Dim webRequest As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
webResponse = DirectCast(webRequest.GetResponse(), Net.HttpWebResponse)
Return True
Catch
Return False
Finally
If webResponse IsNot Nothing Then webResponse.Close()
End Try
End Function

1 comment:

Anonymous said...

hi..
Have a good library.