如何利用JavaScript正则表达式验证网址
2016-11-18
展开全部
用JavaScript的正则表达式验证网址格式是否正确,方法很多,这只是一个实现的方法.
代码如下:
<script type="text/javascript">
function IsUrl(str){
var regUrl = /(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?/gi;
var result = str.match(regUrl);
if(result!=null) {alert( "Yes ")}
else {alert( "No! ")}
}
</script>
<script type="text/javascript ">
function IsUrl(str){
var regUrl = /(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?/gi;
var result = str.match(regUrl);
if(result!=null) {alert( "Yes ")}
else{alert( "No! ")}
}
</script>
调用方法:
<Form name= "bao ">
<input TYPE= "text " NAME= "txt ">
<Input TYPE=BUTTON value= "TestURL " onClick= "IsUrl(document.bao.txt.value) ">
</FORM>
代码如下:
<script type="text/javascript">
function IsUrl(str){
var regUrl = /(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?/gi;
var result = str.match(regUrl);
if(result!=null) {alert( "Yes ")}
else {alert( "No! ")}
}
</script>
<script type="text/javascript ">
function IsUrl(str){
var regUrl = /(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?/gi;
var result = str.match(regUrl);
if(result!=null) {alert( "Yes ")}
else{alert( "No! ")}
}
</script>
调用方法:
<Form name= "bao ">
<input TYPE= "text " NAME= "txt ">
<Input TYPE=BUTTON value= "TestURL " onClick= "IsUrl(document.bao.txt.value) ">
</FORM>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询