Android Question Valid URL

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I tried Erels code (https://www.b4x.com/android/forum/threads/validate-a-url-website-name-in-edittext.112478/) for checking for a valid URL

The one I was checking is this

So I made the following changes to the routine
IsValidUrl:
Sub IsValidUrl(Url As String) As Boolean
    Dim Pattern As String = $"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"$
    Return Regex.IsMatch(Pattern, Url)
End Sub

NOT sure if it will handle everything. But mostly I am checking file names and it works for them
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
You haven't specified what you want to use the URL for. URLs can be extremely complex, and finding a RegEx-based solution that always works correctly is a significant challenge. However, you could use 'Inline Java' to leverage Android/Java's standard `java.net.URI` class. Using AI, I created a B4J inline Java Proof of Concept (POC) capable of verifying the validity of URLs pointing to various images and documents. Given Java's large user base, I believe there is a lower risk of receiving incorrect test results, in addition to a lighter load on system resources.

AI report how it Works Under the Hood
  1. URI(urlString): The native Java environment breaks the string down into its official structural components (scheme, host, path, etc.). If characters are out of place or illegal, it throws an error immediately.
  2. uri.getScheme(): Isolates the prefix. This prevents things like ftp:// or file links from slipping through if you only want standard web traffic.
  3. uri.getHost(): Ensures that a root server location or domain name actually exists in the string (e.g., prevents just https:// from being marked as valid).
Addition:
Given the link you provided as an example, I assume you want to download a PDF file to test with the POC. I deliberately used a download from PortableApps for my testing to demonstrate why certain approaches work or don't work. Below, you will find details of the tests performed and explanations of the results, helping you choose the best solution for your program. Without a brief code example, no one can tell you whether this inline Java or your Regex formula will work.

The result for the correctness of the url 'http..://../anything' = false
Reason: It is http:// or https:// no spces

The result for the correctness of the url 'http://../anything' = false
Reason: 'http://../anything' has missing hostname

The result for the correctness of the url 'https://example.com/anything' = true
The result for matches allowed image extensions of web URL: https://example.com/anything = false
The result for allowed Docs document extensions for web URL: https://example.com/anything = false

Reason: Valid url, however no valid download link on second line

The result for the correctness of the url 'https://portableapps.com/downloadin...=PortableApps.com_Platform_Setup_30.5.paf.exe' = true
The result for matches allowed image extensions of web URL: https://portableapps.com/downloadin...=PortableApps.com_Platform_Setup_30.5.paf.exe = false
The result for allowed Docs document extensions for web URL: https://portableapps.com/downloadin...=PortableApps.com_Platform_Setup_30.5.paf.exe = false

Reason: exe is not given as a valid extension.
Fix: Add on line 133 the extension "exe"
Dim AllowedDocs() As String = Array As String("pdf", "html", "htm")
To or add the extension you want to download like 'zip'
Dim AllowedDocs() As String = Array As String("pdf", "html", "htm", "exe")

The result for the correctness of the url 'https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe' = true
The result for matches allowed image extensions of web URL: https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe = false
The result for allowed Docs document extensions for web URL: https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe = false
Reason: The difference between the two links lies in their function: one is a landing page URL that initiates a download script, while the other is a direct link to the static file on a download server

The result for the correctness of the url 'https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe' = true
The result for matches allowed image extensions of web URL: https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe = false
The result for allowed Docs document extensions for web URL: https://download2.portableapps.com/.../PortableApps.com_Platform_Setup_30.5.paf.exe = true
 

Attachments

  • UrlValidation.zip
    4.3 KB · Views: 12
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I have a webpage and I need to extract that file name url to download the file.

Have no trouble pulling out the file name but thought I should check if it was a valid url.
But in reality if the download fails .... I know it is bad.
 
Upvote 0
Top