简体   繁体   中英

javascript - validate relative url

What is regexp to validate relative url?

In this link

On above link, for relative url: (/[\w~,;\-\./?%&+#=]*)

i edit for validate

var pattern = '/\/[^\w~,;\-\.\/?%&+#=]*/i';
if (!link.match(new RegExp(/\/[^\w~,;\-\.\/?%&+#=]*/i)))
{
    alert('invalid relative link');
}

Did i wrong? how to validate both absolute & relative link?

the reality is anything is a valid relative URL in general! In the context of website http://abc.com for instance, a is a relative url and it expands to http://abc.com/a ; 23$rt% is just as valid and it expands to http://abc.com/23 $rt%. If you mean whether you want to check if the link exists that I suppose you need to employ some Ajax code to assemble the full url and try to fetch it in the background and check the error code (eg, 200 OK means the url is good. 404 is not found etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM