adsense

Saturday, August 7, 2021

.net URL validator

If you need to validate an URL starting with http or https you can simply use the following code snippet.

 

    Public Function ValidHttpURL(ByVal inputURL As String) As Boolean
        If Not Regex.IsMatch(inputURL, "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$", RegexOptions.IgnoreCase) Then
            Return False
        End If
        Return True
    End Function

 

following are the valid test results for above regex

 https://www.google.com -PASS
http://www.google.com - PASS
www.google.com - FAIL
google.com - FAIL
htt://www.google.com - FAIL
http://google.com - PASS

cheers

Samitha

 



No comments:

Post a Comment