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.