Regular expression for URL validation (in JavaScript)
The actual URL syntax is pretty complicated and not easy to represent in regex. Most of the simple-looking regexes out there will give many false negatives as well as false positives. See for amusement these efforts but even the end result is not good.
Plus these days you would generally want to allow IRI as well as old-school URI, so we can link to valid addresses like:
http://en.wikipedia.org/wiki/Þ
http://例え.テスト/
I would go only for simple checks: does it start with a known-good method: name? Is it free of spaces and double-quotes? If so then hell, it's probably good enough. JS Regex url validation
I change the function to Match + make a change here with the slashes and its work: (http(s)?://.)
The fixed function:
function isUrlValid(userInput) {
var res = userInput.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
if(res == null)
return false;
else
return true;
}
Javascript: Website url validation with regex
Even if this answer is a bit too much for this Problem, it illustrates the problem: Even if it might be possible to create a regexp to check the url, it is much simpler and more robust to parse the URL and "create a real Object", on/with which the overall test can be decomposed to a number of smaller tests.
So probably the builtin URL
constructor of modern browsers may help you here (LINK 1, LINK 2).
One approach to test you url might look like this:
function testURL (urlstring) {
var errors = [];
try {
var url = new URL(urlstring);
if (!/https/.test(url.protocol)) {
errors.push('wrong protocol');
}
//more tests here
} catch(err) {
//something went really wrong
//log the error here
} finally {
return errors;
}
}
if (testURL('mr.bean').length == 0) { runSomething(); }
Regex for website or url validation
Use the regex ^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$
This is a basic one I build just now. A google search can give you more.
Here
- ^ Should start with
- ((https?|ftp|smtp)://)? may or maynot contain any of these protocols
- (www.)? may or may not have www.
- [a-z0-9]+(.[a-z]+) url and domain and also subdomain if any upto 2 levels
- (/[a-zA-Z0-9#]+/?)*/? can contain path to files but not necessary. last may contain a
/
- $ should end there
var a=["http://www.sample.com","https://www.sample.com/","https://www.sample.com#","http://www.sample.com/xyz","http://www.sample.com/#xyz","www.sample.com","www.sample.com/xyz/#/xyz","sample.com","sample.com?name=foo","http://www.sample.com#xyz","http://www.sample.c"];var re=/^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/;a.map(x=>console.log(x+" => "+re.test(x)));
Related Topics
Invoke a Callback at the End of a Transition
Uncaught Typeerror: Cannot Use 'In' Operator to Search for 'Length' In
How to Getting Browser Current Locale Preference Using JavaScript
Counting Frequency of Characters in a String Using JavaScript
Javascript: Dynamically Creating Variables for Loops
React-Router Getting This.Props.Location in Child Components
How to Invoke "Click" Event Programmatically in D3
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript
What's the API Key for in Google Maps API V3
How to Load All Files in a Directory Using Webpack Without Require Statements
Detect 64-Bit or 32-Bit Windows from User Agent or JavaScript
How to Bind Bootstrap Popover on Dynamic Elements
Using Filesystem in Node.Js with Async/Await