File size: 2,503 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
function isIDN( url: string ): boolean {
try {
// Regex to extract the hostname from the URL.
const urlRegex = /^(?:https?:\/\/)?(?:www\.)?([^/]+)/i;
const match = url.match( urlRegex );
if ( ! match ) {
return false;
}
// Extract the hostname.
const hostname = match[ 1 ];
// Check for non-ASCII characters
for ( let i = 0; i < hostname.length; i++ ) {
if ( hostname.charCodeAt( i ) > 127 ) {
return true;
}
}
// Check if the hostname starts with the Punycode prefix.
return hostname.startsWith( 'xn--' );
} catch ( e ) {
return false;
}
}
/*
* Get the validation message for the given URL.
*
* @param {string} url The URL to validate.
* @param {Function} translate The translation function.
* @param {Function} hasEnTranslation The function to check if a translation key has an English translation.
*
* @return {string} The validation message for the given URL.
*/
export default function getValidationMessage( url: string, translate: ( key: string ) => string ) {
const missingTLDRegex = /^(?:https?:\/\/)?(?!.*\.[a-z]{2,})([a-zA-Z0-9-_]+)$/;
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const invalidURLProtocolRegex = /^(?!https?:\/\/)\S+:\/\//;
const invalidCharsRegex = /[^a-z0-9\-._!$&'()*+,;:@/?=%[\]]/i;
const removedInitialDots = url.replace( 'www.', '' );
let errorMessage = translate(
"Please add a valid website address (like 'example.com'). Feel free to copy and paste it in if that helps."
);
if ( emailRegex.test( url ) ) {
errorMessage = translate(
"Looks like you might have added an email address. Please use a URL instead, like 'example.com'."
);
} else if ( isIDN( url ) ) {
errorMessage = translate(
"Looks like you’ve added an internationalized domain name. Please try a standard URL instead (like 'example.com')."
);
} else if ( invalidCharsRegex.test( url ) ) {
errorMessage = translate(
'Looks like your URL has some invalid characters (like ~ or ^). Please delete them and try again.'
);
} else if ( missingTLDRegex.test( removedInitialDots ) ) {
errorMessage = translate(
"Looks like your site address is missing its domain extension. Please try again with something like 'example.com' or 'example.net'."
);
} else if ( invalidURLProtocolRegex.test( url ) ) {
errorMessage = translate(
"URLs usually start with http:// or https:// (rather than file:/, ftp://, or similar). Please try again with a URL like 'https://example.com'."
);
}
return errorMessage;
}
|