Skip to content
Prev Previous commit
Next Next commit
Match the URL validation from customizer
  • Loading branch information
joedolson committed Mar 6, 2025
commit 92ba5de440ad1fd5f761968ca1bc1fe8d7ba55ac
15 changes: 14 additions & 1 deletion src/js/_enqueues/lib/nav-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,20 @@
errorMessage.hide();
urlWrap.removeClass( 'has-error' );

if ( '' === url || 'https://' === url || 'http://' === url ) {
/*
* Allow URLs including:
* - http://example.com/
* - //example.com
* - /directory/
* - ?query-param
* - #target
* - mailto:foo@example.com
*
* Any further validation will be handled on the server when the setting is attempted to be saved,
* so this pattern does not need to be complete.
*/
urlRegex = /^((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#)/;
if ( ! urlRegex.test( url ) ) {
e.preventDefault();
urlInput.addClass( 'form-invalid' )
.attr( 'aria-invalid', 'true' )
Expand Down
Loading