Using RegEx To Validate A URL

In my last post I shared an example of how we can use RegEx to validate a Password to ensure it followed some minimum rules we set forth, to learn more simply consult:

Today, I thought I’d share another RegEx example to validate URLs! This is a great way to control with what users input in your forms and hopefully ensure that they are valid.
 

The Pattern, Always The Pattern

As with all testing RegEx code, we follow the same setup as presented in the above article. Once again the only variation here is the Pattern used for testing/validation of the URL string we pass to it.

If you search online you will find a multitude of variation and this is because it all depends on things like:

  • do you want to oblige the presence of the http:// or https://
  • do you want to oblige the presence of the www.
  • so on an so forth

As such, I present to you 4 potential pattern for you to test and see if they meet your need.

(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%\._\\\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\\+\.~#?&=]*)
(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?\/[a-zA-Z0-9]{2,}|((https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?)|(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}(\.[a-zA-Z0-9]{2,})?
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)

You’ll can also find some really nice example like:

^(?P[a-z\-]+)?:?(?P\/\/.*\.[^\/]+)$

but these although better, do not seem to work in VBA’s implementation of RegEx.
 

The Validation Function

The code itself is always basically a copy/paste of any of my other validation functions, simply changing the pattern used. So we end up with:

'---------------------------------------------------------------------------------------
' Procedure : IsValidURL
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Validates a URL
'               It returns True if it is valid, otherwise it returns False
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL    : The URL you wish to validate
'
' Usage:
' ~~~~~~
' IsValidURL("https://www.google.com")
'   Returns => True/False
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2024-02-04              Initial Release
'---------------------------------------------------------------------------------------
Public Function IsValidURL(ByVal sURL As Variant) As Boolean
    On Error GoTo Error_Handler
    Dim oRegEx          As Object
 
    If Not IsNull(sURL) Then
        Set oRegEx = CreateObject("VBScript.RegExp")
        'oRegEx.pattern = "(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%\._\\\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\\+\.~#?&=]*)"
        'oRegEx.pattern = "(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?\/[a-zA-Z0-9]{2,}|((https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?)|(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}(\.[a-zA-Z0-9]{2,})?"
        'oRegEx.pattern = "(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})"
        oRegEx.pattern = "https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)"
        'oRegEx.pattern = "^(?P<protocol>[a-z\-]+)?:?(?P<host>\/\/.*\.[^\/]+)$" 'Doesn't work!
        IsValidURL = oRegEx.test(sURL)
    End If
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oRegEx Is Nothing Then Set oRegEx = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: IsValidURL" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

Simply comment/uncomment the pattern you wish to use, or try another.

Then you can simply use it by doing:

? IsValidURL("https://google.com") 'Returns True
? IsValidURL("google.com") 'Returns False
? IsValidURL("https://www.google.com") 'Returns True
? IsValidURL(Me.URL) 'Returns True/False depending the value