Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit f42d0ac

Browse files
Nicklas Ansman GiertzNicklas Ansman Giertz
authored andcommitted
Update the regexp for the URL validator
1 parent cffa61a commit f42d0ac

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

specs/validators/url-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ describe("validators.url", function() {
4747
expect(url("http://foo.bar/foo(bar)baz quux", {})).toBeDefined();
4848
expect(url("ftps://foo.bar/", {})).toBeDefined();
4949
expect(url("http://-error-.invalid/", {})).toBeDefined();
50-
expect(url("http://a.b--c.de/", {})).toBeDefined();
5150
expect(url("http://-a.b.co", {})).toBeDefined();
5251
expect(url("http://a.b-.co", {})).toBeDefined();
5352
expect(url("http://0.0.0.0", {})).toBeDefined();
@@ -105,6 +104,7 @@ describe("validators.url", function() {
105104
expect(url("http://1337.net", {})).not.toBeDefined();
106105
expect(url("http://a.b-c.de", {})).not.toBeDefined();
107106
expect(url("http://223.255.255.254", {})).not.toBeDefined();
107+
expect(url("http://a.b--c.de/", {})).not.toBeDefined();
108108
});
109109

110110
it("allows local url and private networks if option is set", function() {

validate.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,48 +1095,46 @@
10951095
// https://gist.github.com/dperini/729294
10961096
var regex =
10971097
"^" +
1098-
// schemes
1099-
"(?:(?:" + schemes.join("|") + "):\\/\\/)" +
1100-
// credentials
1101-
"(?:\\S+(?::\\S*)?@)?";
1102-
1103-
regex += "(?:";
1098+
// protocol identifier
1099+
"(?:(?:" + schemes.join("|") + ")://)" +
1100+
// user:pass authentication
1101+
"(?:\\S+(?::\\S*)?@)?" +
1102+
"(?:";
11041103

11051104
var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";
11061105

1107-
// This ia a special case for the localhost hostname
11081106
if (allowLocal) {
11091107
tld += "?";
11101108
} else {
1111-
// private & local addresses
11121109
regex +=
1113-
"(?!10(?:\\.\\d{1,3}){3})" +
1114-
"(?!127(?:\\.\\d{1,3}){3})" +
1115-
"(?!169\\.254(?:\\.\\d{1,3}){2})" +
1116-
"(?!192\\.168(?:\\.\\d{1,3}){2})" +
1117-
"(?!172" +
1118-
"\\.(?:1[6-9]|2\\d|3[0-1])" +
1119-
"(?:\\.\\d{1,3})" +
1120-
"{2})";
1121-
}
1122-
1123-
var hostname =
1124-
"(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
1125-
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
1126-
tld + ")";
1127-
1128-
// reserved addresses
1110+
// IP address exclusion
1111+
// private & local networks
1112+
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
1113+
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
1114+
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})";
1115+
}
1116+
11291117
regex +=
1118+
// IP address dotted notation octets
1119+
// excludes loopback network 0.0.0.0
1120+
// excludes reserved space >= 224.0.0.0
1121+
// excludes network & broacast addresses
1122+
// (first & last IP address of each class)
11301123
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
11311124
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
11321125
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
11331126
"|" +
1134-
hostname +
1135-
// port number
1136-
"(?::\\d{2,5})?" +
1137-
// path
1138-
"(?:[/?#]\\S*)?" +
1139-
"$";
1127+
// host name
1128+
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
1129+
// domain name
1130+
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
1131+
tld +
1132+
")" +
1133+
// port number
1134+
"(?::\\d{2,5})?" +
1135+
// resource path
1136+
"(?:[/?#]\\S*)?" +
1137+
"$";
11401138

11411139
var PATTERN = new RegExp(regex, 'i');
11421140
if (!PATTERN.exec(value)) {

0 commit comments

Comments
 (0)