Javascript verifies that the URL address entered by the user is null and properly formatted

  • 2020-03-30 04:06:06
  • OfStack


<script type="text/javascript">
function checkUrl() {
var url = document.getElementById('url').value;
if (url==''){
alert('URL The address cannot be empty ');
return false;
} else if (!isURL(url)) {
alert('URL The format should be //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.(last one. At least one character in front) and. At least one word character in the back. The last character must be a word character or /
varreg=/w+.(w+|w+/)$/;
varisurl=reg.test(str);//The test(STR) method is an object built into the correct js expression that can be called
directly returnisurl;
}
</script>


<form action="" method="post" onsubmit="checkUrl();"> URL:
<input type="text" name="url" id="url" value="//www.jb51.net" onfocus="this.value=''"/>
<br /><br />
<input type="submit" value=" Get the address " name="get"/>
<input type="submit" value=" download " name="download"/>
</form>

PHP detects the validity of the URL address

The following code


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

If we want to see if we can do this properly we can use the file_get_contents() function to verify.


Related articles: