Home > Web Front-end > JS Tutorial > body text

How to determine whether the value is a url address in jquery

coldplay.xixi
Release: 2020-11-20 11:11:18
Original
2827 people have browsed it

Jquery method to determine whether the value is a url address: first create an input box and use jquery to register a change event for it; then create a function checkUrl and pass in the url parameter; then write a regular strRegex in the function to detect the correctness of the url. ;Finally, use the test method to detect the url parameters.

How to determine whether the value is a url address in jquery

How jquery determines whether a value is a url address:

Use the test() method to detect url parameters That’s it.

Note: Only important code is written

<script src="./jquery.js"></script>
<div id="app" > 
    <input type="text" id="url" >
</div>
<script>
    // 本代码来自 https://blog.csdn.net/eddy23513/article/details/80194269
    function checkUrl(url) {
        var strRegex = &#39;^((https|http|ftp|rtsp|mms)?://)?&#39;
            +&#39;(([0-9a-z_!~*().&=+$%-]+: )?[0-9a-z_!~*().&=+$%-]+@)?&#39; //ftp的user@
            +&#39;(([0-9]{1,3}.){3}[0-9]{1,3}|&#39;// IP形式的URL- 199.194.52.184
            +&#39;([0-9a-z_!~*()-]+.)*&#39;// 域名- www.
            +&#39;[a-z]{2,6})&#39;//域名的扩展名
            +&#39;(:[0-9]{1,4})?&#39;// 端口- :80
            +&#39;((/?)|(/[0-9a-z_!~*().;?:@&=+$,%#-]+)+/?)$&#39;;
        return new RegExp(strRegex).test(url);
    }
   // 输入框注册事件
    $(&#39;#url&#39;).change(function(){
        let res = checkUrl(this.value)
        console.log(this.value, res)
    })
</script>
Copy after login

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to determine whether the value is a url address in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template