How do Javascript and PHP verify that the user entered the URL address correctly?
This article mainly explains the "Javascript and PHP how to verify whether the user entered the URL address is correct", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Javascript and PHP how to verify whether the user entered the URL address is correct" bar!
1.javascript detects the validity of URL addresses:
The copy code is as follows:
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 https://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 include. The last one. There is at least one character in front of it. There is at least one word character after it, and the last character must be a word character or /
Varreg=/w+. (w + | w /) $/
The varisurl=reg.test (str); / / test (str) method is the js correct expression built-in object that can be called directly
Returnisurl
}
2.PHP detects the validity of URL addresses
The copy code is as follows: