开发者

javascript - validate relative url

开发者 https://www.devze.com 2023-03-01 01:18 出处:网络
What is regexp to validate relative url ? In this link On above link , for relative url : (/[\\w~,;\\-\\./?%&+#=]*)

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 (e.g. 200 OK means the url is good, 404 is not found etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消