We sometimes need to check the validity of the Web site (URL) entered by the user,
Copy Code code as follows:
function Checkurl (str) {
var regurl = new RegExp ();
Regurl.compile ("^[a-za-z]+://[a-za-z0-9-_]+\.[ a-za-z0-9-_%&?/.=]+$ ");
if (! Regurl.test (str)) {
return false;
}
return true;
}
Not only from the format, but also to check whether the user entered the URL is really valid. Check the URL can refer to the JS regular expression validation URL format, check the URL effective method There are many, for example, you can use jquery to check,
This article will give a section to use C # to determine whether the URL is valid code.
Below is a way to check the validity of URLs:
Copy Code code as follows:
private bool Urlcheck (string strURL)
{
if (!strurl.contains ("http://") &&!strurl.contains ("https://")
{
strURL = "http://" + strurl;
}
Try
{
HttpWebRequest myrequest = (HttpWebRequest) webrequest.create (strURL);
Myrequest.method = "Head";
Myrequest.timeout = 10000; Timeout time 10 seconds
HttpWebResponse res = (HttpWebResponse) myrequest.getresponse ();
Return (Res. StatusCode = = Httpstatuscode.ok);
}
Catch
{
return false;
}
}
When used, we only need to pass in the URL for the validity check, when using this method remember to add the System.Net namespace.
Copy Code code as follows:
if (! Urlcheck ("Http://www.jb51.net"))
{
Delete operation
}
Yes, in this site users can enter their own web site, but some users do not know is to test or what, entered a glance can be seen is not access to the link. So it is necessary for users to enter the URL to do a check, otherwise invalid links too many words will affect the weight of the site, more importantly, too many invalid links will affect the experience of other users, because who do not want to click an invalid link.
You can refer to jquery Ajax,
The following 5 methods perform a short form of a general Ajax request and should use Jquery.ajax () when dealing with complex AJAX requests.
1.load (Url,[data],[callback])
Loads the remote HTML file code and inserts it into the DOM, using the Get method by default, which is automatically converted to post when the parameter is passed.
◦url: Remote URL address to load
◦data: Key/value data sent to the server
◦callback: callback function when load succeeds
The sample code is as follows:
Copy Code code as follows:
No parameters, no callback functions
$ ("#showload"). Load ("load.htm");
No callback function
$ ("#showload"). Load ("load.htm", {"para": "Para-value"});
$ ("#showload"). Load ("load.htm", {"para": "Para-value"},
function () {
Processing
})