Robert Valentino Well-Known Member Licensed User Longtime User Sep 13, 2026 #1 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 https://pdf.leaguesecretary.com/uploads/2026/f/4/14551509112026f202604standg00.pdf So I made the following changes to the routine Code: 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
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 https://pdf.leaguesecretary.com/uploads/2026/f/4/14551509112026f202604standg00.pdf So I made the following changes to the routine Code: 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 Sep 14, 2026 #2 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 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. uri.getScheme(): Isolates the prefix. This prevents things like ftp:// or file links from slipping through if you only want standard web traffic. 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 Click to expand... Reason: It is http:// or https:// no spces The result for the correctness of the url 'http://../anything' = false Click to expand... 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 Click to expand... 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 Click to expand... 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") Click to expand... To or add the extension you want to download like 'zip' Dim AllowedDocs() As String = Array As String("pdf", "html", "htm", "exe") Click to expand... 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 Click to expand... 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 Click to expand... Attachments UrlValidation.zip UrlValidation.zip 4.3 KB · Views: 75 Last edited: Sep 14, 2026 Upvote 0
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 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. uri.getScheme(): Isolates the prefix. This prevents things like ftp:// or file links from slipping through if you only want standard web traffic. 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 Click to expand... Reason: It is http:// or https:// no spces The result for the correctness of the url 'http://../anything' = false Click to expand... 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 Click to expand... 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 Click to expand... 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") Click to expand... To or add the extension you want to download like 'zip' Dim AllowedDocs() As String = Array As String("pdf", "html", "htm", "exe") Click to expand... 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 Click to expand... 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 Click to expand...
TILogistic Expert Licensed User Longtime User Sep 14, 2026 #3 ? [B4X] Extract the structure of a URL I share a simple routine that allows you to extract the structure of a URL. Note: In the "path" section it allows you to extract the parameters of the pretty URL and "parameters" those of a normal URL. See the examples URL tests Dim URL As String =... www.b4x.com Url exist or not Hi, I am looking for code to check if a url still exists so that if it does not exist I will not go there www.b4x.com Upvote 0
? [B4X] Extract the structure of a URL I share a simple routine that allows you to extract the structure of a URL. Note: In the "path" section it allows you to extract the parameters of the pretty URL and "parameters" those of a normal URL. See the examples URL tests Dim URL As String =... www.b4x.com Url exist or not Hi, I am looking for code to check if a url still exists so that if it does not exist I will not go there www.b4x.com
Robert Valentino Well-Known Member Licensed User Longtime User Sep 14, 2026 #4 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
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.
ilan Expert Licensed User Longtime User Sep 15, 2026 #5 try this: B4X: Sub IsValidUrl(Url As String) As Boolean Dim Pattern As String = $"(https?:\/\/[^\s]+)"$ Return Regex.IsMatch(Pattern, Url) End Sub Upvote 0
try this: B4X: Sub IsValidUrl(Url As String) As Boolean Dim Pattern As String = $"(https?:\/\/[^\s]+)"$ Return Regex.IsMatch(Pattern, Url) End Sub