"JavaScript" uses regular expressions to check if the input box is a URL

Source: Internet
Author: User

This function is also very common in the Web page, the link part, the form fills in the personal homepage, uses the JavaScript to verify whether is the URL.

This test is not good to write, it is best to use regular expressions to authenticate.

Specifies that the input can only be http://And https://, and must be a URL.

Some people say, why is a webpage like www.1.com not?

This is in case you take the user input to construct a hyperlink, the href attribute in the A tag if you do not encounter http://or https://, then you will think it is the root directory, will be in your website after the URL to write this address and then jump, this people should know. For example <a href= "www.1.com" >XXX</A>, my URL is http://localhost, then click on this display as XXX's a tag, then just jump to http://localhost/ Www.1.com This position, of course, is wrong.

For example, the following text box, how to use the regular expression to require the user input must be http://And https://the beginning of the URL?


1, the first is a simple layout, this needless to say:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

2, followed by the script, in fact, needless to say, the key is that regular expression:

<script>function Checkurl () {var url=document.getelementbyid ("url"). Value;var reg=/^ ([hh][tt]{2}[pp]:\/\/|[ hh][tt]{2}[pp][ss]:\/\/) (([a-za-z0-9-~]+) \.) + ([a-za-z0-9-~\/]) +$/;if (!reg.test (URL)) {alert ("This URL does not start with http://https://, or it is not a URL! ");} Else{alert ("input succeeded");}} </script>

In:

var reg=/^ ([hh][tt]{2}[pp]:\/\/|[ hh][tt]{2}[pp][ss]:\/\/) (([a-za-z0-9-~]+) \.) + ([a-za-z0-9-~\/]) +$/;
Among

1, JavaScript, because all variables are Var, so the regular expression must be written in two slash,/.../, and then the regular expression inside the slash/must be written/

2. ^ denotes the need to ... The beginning, [] denotes an inspection unit, that is, something that a character can hold, such as ^ ([hh][tt]{2}[pp]:\/\/|[ hh][tt]{2}[pp][ss]:\/\/) is the requirement to start with http://or https://. | Yes or, the first character is H or H, and the second to third character is [tt],{2} is the 1 characters that are included with this character must be [TT] Meaning, and so on and so on

3, ([a-za-z0-9-~]+) said, including this character and its subsequent characters must be uppercase letters, lowercase letters, numbers, minus-or a ~

Character + means: match the character preceding the + number 1 or n times, for example:/a+/matches all ' a ' in ' a ' and ' Caaaaaaandy ' in ' Candy '.

4, therefore ([a-za-z0-9-~]+) \.) + it means xxx. This end-of-the-point thing must appear at least 1 times before ([a-za-z0-9-~\/]) +$ this character

5, $ means must be in uppercase letters, lowercase letter, numbers, minus-, ~,/End

"JavaScript" uses regular expressions to check if the input box is a URL

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.