Javascript验证用户输入URL地址是否为空及格式是否正确
<scripttype="text/javascript">
functioncheckUrl(){
varurl=document.getElementById('url').value;
if(url==''){
alert('URL地址不能为空');
returnfalse;
}elseif(!isURL(url)){
alert('URL的格式应该是https://www.nhooo.com');
returnfalse;
}else{
returntrue;
}
returnfalse;
}
functionisURL(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>