Javascript verifies that the URL entered by the user is correct

Source: Internet
Author: User

To enable the user to enter a URL, we need to verify that the URL entered by the user is correct before the user submits the URL. This JavaScript verification will not confirm whether the URL is valid, but it must be saved correctly. Code

 

The Code is as follows: Copy code

<Script type = "text/javascript">
Function checkUrl (){
Var url = document. getElementById ('url'). value;
If (url = ''){
Alert ('url address cannot be blank ');
Return false;
} Else if (! IsURL (url )){
Alert ('url format should be http://www.bKjia. c0m ');
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) $ /;
// It must contain at least one character. (the last one has at least one character.) and 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 js correct expression. The built-in object can be directly called.
Returnisurl;
}
</Script>


<Form action = "" method = "post" onsubmit = "checkUrl ();">
URL:
<Input type = "text" name = "url" id = "url" value = "http://www.bKjia. c0m" onfocus = "this. value ='' "/>
<Br/>
<Input type = "submit" value = "get address" name = "get"/>
<Input type = "submit" value = "download" name = "download"/>
</Form>

PHP checks URL address Validity

The Code is as follows: Copy code

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

If you want to try a normal method, you can use the file_get_contents () function to verify it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.