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 的格式應該是 http://www.111cn.net');
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正確表達式內置的對象可以(www.111cn.net)直接調用
returnisurl;
}
</script>


<form action="" method="post" onsubmit="checkUrl();">
URL:
<input type="text" name="url" id="url" value="http://www.111cn.net" onfocus="this.value=''"/>
<br /><br />
<input type="submit" value="獲取地址" name="get"/>
<input type="submit" value="下載" name="download"/>
</form>

PHP檢測URL地址有效性

代碼如下 復制代碼
<?
function is_url($str){
return preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>"])*$/", $str);
}
?>

如果要嘗試是否可以正常方法我們可使用file_get_contents()函數來驗證就可以了。
from:http://www.111cn.net/wy/js-ajax/50084.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM