


Javascript verifies whether the URL address entered by the user is empty and the format is correct_javascript skills
May 16, 2016 pm 04:34 PM<script type="text/javascript">
function checkUrl() {
var url = document.getElementById('url').value;
if (url==''){
alert('URL address cannot be empty');
return false;
} else if (!isURL(url)) {
alert('The format of URL should be http://www.jb51.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)$/;
//Must contain . (the last . is preceded by at least one character) and . is followed by at least one word character. The last character must be a word character or /
varreg=/w .(w |w /)$/;
varisurl=reg.test(str);//The test(str) method is a built-in object of js correct expression and can be called directly
returnisurl;
}
</script>
<form action="" method="post" onsubmit="checkUrl();">
URL:
<input type="text" name="url" id="url" value="http://www.jb51.net" onfocus="this.value=''"/>
<br /><br />
<input type="submit" value="Get address" name="get"/>
<input type="submit" value="Download" name="download"/>
</form>
PHP detects URL address validity
The code is as follows
<?
function is_url($str){
return preg_match("/^http://[A-Za-z0-9] .[A-Za-z0-9] [/=?%-&_~`@[]': !]*([^< ;>"])*$/", $str);
}
?>
If you want to try if it works normally, we can use the file_get_contents() function to verify it.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed method to unblock using WeChat friend-assisted verification

How to extract URL address using regular expression in Go language

How to validate IFSC code using regular expressions?

New features in PHP 8: Added verification and signing

How to verify whether input is uppercase letters in golang

How to verify whether the input is full-width characters in golang
