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.

le noob
  • 93
  • 1
  • 4
  • That's exactly what I wanted to know. I thought I was just crazy. Thanks! – le noob Jul 17 '12 at 18:14
  • If you don't mind, I'd like to mark your comment as the answer. ;) – le noob Jul 17 '12 at 18:14
  • 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. – jfriend00 Jul 17 '12 at 18:17
  • @jfriend00 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! – le noob Jul 17 '12 at 18:35

3 Answers3

1

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/

Community
  • 1
  • 1
Dominic Green
  • 10,142
  • 4
  • 30
  • 34
  • 3
    The jQuery validation library includes support for URLs http://docs.jquery.com/Plugins/Validation/Methods/url. However, not all URIs are URLs. – koppor Apr 02 '13 at 15:59
  • this is invalid; gives false positive for `https://df.h.` – avalanche1 Feb 07 '21 at 06:12
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"

koppor
  • 19,079
  • 15
  • 119
  • 161
-1

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

codeKnight
  • 241
  • 2
  • 12