Skip to content

Commit f33690b

Browse files
committed
Core: Replace regex for url method
Making private IPs invalid is fine, and I've modified it to keep allowing protocol-relative urls. Fixes jquery-validation#1426
1 parent 7221726 commit f33690b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/core.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,12 @@ $.extend( $.validator, {
11811181

11821182
// http://jqueryvalidation.org/url-method/
11831183
url: function( value, element ) {
1184-
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
1185-
return this.optional( element ) || /^((https?|s?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 );
1184+
1185+
// Copyright (c) 2010-2013 Diego Perini, MIT licensed
1186+
// https://gist.github.com/dperini/729294
1187+
// see also https://mathiasbynens.be/demo/url-regex
1188+
// modified to allow protocol-relative URLs
1189+
return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value );
11861190
},
11871191

11881192
// http://jqueryvalidation.org/date-method/

test/methods.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ test("url", function() {
3737
ok( method( "ftp://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
3838
ok( method( "http://www.føtex.dk/" ), "Valid url, danish unicode characters" );
3939
ok( method( "http://bösendorfer.de/" ), "Valid url, german unicode characters" );
40-
ok( method( "http://192.168.8.5" ), "Valid IP Address" );
40+
ok( method( "http://142.42.1.1" ), "Valid IP Address" );
4141
ok( method( "http://pro.photography" ), "Valid long TLD" );
4242
ok( method( "//code.jquery.com/jquery-1.11.3.min.js" ), "Valid protocol-relative url" );
43-
ok( method( "//192.168.8.5" ), "Valid protocol-relative IP Address" );
43+
ok( method( "//142.42.1.1" ), "Valid protocol-relative IP Address" );
4444
ok(!method( "htp://code.jquery.com/jquery-1.11.3.min.js" ), "Invalid protocol" );
4545
ok(!method( "http://192.168.8." ), "Invalid IP Address" );
4646
ok(!method( "http://bassistance" ), "Invalid url" ); // valid

0 commit comments

Comments
 (0)