Home Web Front-end JS Tutorial Javascript verifies whether the URL address entered by the user is empty and the format is correct_javascript skills

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
url address verify

Copy code The code is as follows:

<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>


Copy code The code is as follows:

<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

Copy code 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.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

How to verify signature in PDF

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

Detailed method to unblock using WeChat friend-assisted verification

How to extract URL address using regular expression in Go language How to extract URL address using regular expression in Go language Jul 14, 2023 pm 10:00 PM

How to extract URL address using regular expression in Go language

How to obtain url address How to obtain url address Jul 19, 2023 am 09:37 AM

How to obtain url address

How to validate IFSC code using regular expressions? How to validate IFSC code using regular expressions? Aug 26, 2023 pm 10:17 PM

How to validate IFSC code using regular expressions?

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

New features in PHP 8: Added verification and signing

How to verify whether input is uppercase letters in golang How to verify whether input is uppercase letters in golang Jun 24, 2023 am 09:06 AM

How to verify whether input is uppercase letters in golang

How to verify whether the input is full-width characters in golang How to verify whether the input is full-width characters in golang Jun 25, 2023 pm 02:03 PM

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

See all articles