URI validation in JavaScript
Asked Answered
C

3

7

What is a robust way (doesn't have to be regex, does it?) to validate that a URI works on Android by only using JavaScript?

That is, the check is not done in Java on the Android SDK side; it's done in a webpage using JavaScript.

EDIT: By "works on Android", I mean that Android can find an Activity that responds to an Intent using that URI.

Coset answered 17/7, 2012 at 18:2 Comment(4)
That's exactly what I wanted to know. I thought I was just crazy. Thanks!Coset
If you don't mind, I'd like to mark your comment as the answer. ;)Coset
What do you mean by "validate that a URI works"? Do you mean verify that it's in a valid URI format? Or do you mean that the URI actually point to an accessible web page? The former can be done with javascript that checks for legal format. The latter requires actually downloading the web page to see if it exists and is available.Corkscrew
@Corkscrew That's a great question. I mean that Android has an Activity to respond to an Intent with that URI. I'll update the question. Thanks!Coset
M
0

Javascript is never as robust as backend but there are plenty off decent regex's around.

This is covered in another post here Trying to Validate URL Using JavaScript

The best answer I think is below

Someone mentioned the Jquery Validation plugin, seems overkill if you just want to validate the url, here is the line of regex from the plugin:

return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);

Here is where they got it from: http://projects.scottsplayground.com/iri/

Mussel answered 17/7, 2012 at 18:15 Comment(2)
The jQuery validation library includes support for URLs docs.jquery.com/Plugins/Validation/Methods/url. However, not all URIs are URLs.Jon
this is invalid; gives false positive for https://df.h.Montgomery
J
0

A good regex for URIs (which are a superset of URLs) surely is https://github.com/jhermsmeier/uri.regex. Unfortunately, it does not cover all types of URIs yet. However, it works in Chrome.

A more simpler version is available at http://jmrware.com/articles/2009/uri_regexp/URI_regex.html. Due to multiline, I could not include it here.

And here a step-by-step development with JUnit test cases: http://timezra.blogspot.de/2010/05/regex-to-validate-uris.html This doesn't work here "Invalid regular expression: Invalid group"

Jon answered 2/4, 2013 at 16:6 Comment(0)
D
-2

Simply try calling in your code:

const url = new URL(urlToCheck)

If the given urlToCheck or the resulting URL are not valid URLs,
then exception is thrown..... and then you know...

see:

https://javascript.info/url

https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

Disorganization answered 4/5, 2020 at 13:23 Comment(3)
This is not true. You could enter a non-URL function and it would return without an error.Jiggerypokery
new URL('d:/urlToCheck.com') doesnt throwMontgomery
URI != URL. Not all URIs are URLs. dev.to/flippedcoding/…Petersham

© 2022 - 2024 — McMap. All rights reserved.