Javascript verifies whether the URL entered by the user is null and the format is correct
The Code is as follows:
<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.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) $ /;
// 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>
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/>
<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:
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 a normal method, you can use the file_get_contents () function to verify it.