asp.net C # to check the URL is valid method _ Practical Tips

Source: Internet
Author: User
Tags regular expression

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
})

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.