Javascript与PHP验证用户输入URL地址是否正确
作者:shichen2014 时间:2023-10-23 02:36:07
本文实例讲述了Javascript与PHP验证用户输入URL地址是否正确的方法,分享给大家供大家参考。具体方法如下:
1.javascript检测URL地址有效性:
<script type="text/javascript">
function checkUrl() {
var url = document.getElementById('url').value;
if (url==''){
alert('URL 地址不能为空');
return false;
} else if (!isURL(url)) {
alert('URL 的格式应该是 https://www.aspxhome.com');
return false;
} else {
return true;
}
return false;
}
function isURL(str)
{
//varreg=/[0-9a-zA-z]+.(html|htm|shtml|jsp|asp|php|com|cn|net|com.cn|org)$/;
//必须包含.(最后面一个.前面最少有一个字符)且.后面最少有一个单词字符,最后一个字符必须为单词字符或/
varreg=/w+.(w+|w+/)$/;
varisurl=reg.test(str);//test(str)方法是js正确表达式内置的对象可以直接调用
returnisurl;
}
</script>
<form action="" method="post" onsubmit="checkUrl();">
URL:
<input type="text" name="url" id="url" value="https://www.aspxhome.com" onfocus="this.value=''"/>
<br /><br />
<input type="submit" value="获取地址" name="get"/>
<input type="submit" value="下载" name="download"/>
</form>
2.PHP检测URL地址有效性
<?
function is_url($str){
return preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>"])*$/", $str);
}
?>
如果要尝试是否可以正常方法我们可使用file_get_contents()函数来验证就可以了。
希望本文所述对大家的PHP程序设计有所帮助。
标签:Javascript,PHP,URL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django REST framework 如何实现内置访问频率控制
2023-08-01 17:11:00
TypeScript中命名空间与模块化详情
2024-04-25 13:11:23
![](https://img.aspxhome.com/file/2023/5/135545_0s.png)
python实现简单中文词频统计示例
2022-04-17 16:54:35
![](https://img.aspxhome.com/file/2023/7/101557_0s.png)
Python 窗体(tkinter)下拉列表框(Combobox)实例
2022-04-01 02:38:07
基于pycharm实现批量修改变量名
2022-01-23 05:46:21
![](https://img.aspxhome.com/file/2023/1/131011_0s.png)
解决Pycharm出现的部分快捷键无效问题
2021-09-12 12:49:34
![](https://img.aspxhome.com/file/2023/0/115800_0s.jpg)
perl中chomp的使用介绍(chop和chomp函数区别)
2022-09-01 16:04:18
Python遍历文件夹 处理json文件的方法
2022-02-19 16:08:40
js序列化和反序列化的使用讲解
2023-08-05 08:13:24
使用Python将Mysql的查询数据导出到文件的方法
2024-01-17 21:36:22
Django更新models数据库结构步骤
2024-01-16 09:05:25
![](https://img.aspxhome.com/file/2023/7/133797_0s.jpg)
mysql回表致索引失效案例讲解
2024-01-21 01:28:48
![](https://img.aspxhome.com/file/2023/0/101530_0s.png)
浅谈Python基础之I/O模型
2021-11-17 18:38:03
![](https://img.aspxhome.com/file/2023/1/87671_0s.png)
Web内容写作:得到更好稿件的头15条规则[译]
2011-06-09 14:38:00
python绘制地震散点图
2021-12-21 13:09:08
![](https://img.aspxhome.com/file/2023/5/82545_0s.jpg)
Mootools 1.2教程(5)——事件处理
2008-11-19 16:33:00
linux配置mysql数据库远程连接失败的解决方法
2024-01-28 06:53:59
![](https://img.aspxhome.com/file/2023/1/113981_0s.jpg)
10分钟学会Google Map API (二)
2009-06-07 18:14:00
python实现自动化办公邮件合并功能
2022-02-22 21:06:06
![](https://img.aspxhome.com/file/2023/1/81571_0s.png)
python多线程之事件Event的使用详解
2022-12-21 11:46:10