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 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 = '^((https|http|ftp|rtsp|mms)?://)?' +'(([0-9a-z_!~*().&=+$%-]+: )?[0-9a-z_!~*().&=+$%-]+@)?' //ftp的user@ +'(([0-9]{1,3}.){3}[0-9]{1,3}|'// IP形式的URL- 199.194.52.184 +'([0-9a-z_!~*()-]+.)*'// 域名- www. +'[a-z]{2,6})'//域名的扩展名 +'(:[0-9]{1,4})?'// 端口- :80 +'((/?)|(/[0-9a-z_!~*().;?:@&=+$,%#-]+)+/?)$'; return new RegExp(strRegex).test(url); } // 输入框注册事件 $('#url').change(function(){ let res = checkUrl(this.value) console.log(this.value, res) }) </script>
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!