The function of js regular checking whether the input is a URL is also very common in web pages. When filling in personal homepages with friendly links and forms, use JavaScript to verify whether it is a URL.
This test is not easy to write, so it is better to use regular expressions to authenticate.
It is stipulated that the input can only start with http:// and https://, and must be a URL.
Some people say, why can’t web pages like www.1.com work?
This is to avoid the fact that when you use the user input to construct a hyperlink, if the href attribute in the a tag cannot encounter something http:// or https://, it will be considered as the root directory and will then write this address behind the URL of your website and then jump. Everyone should know this. For example, if <a href="www.1.com">xxx</a>, my URL is http://localhost. Then after clicking on the a tag displayed as xxx, you just jump to the location http://localhost/www.1.com, of course it is wrong.
For example, in the following text box, how to use regular expressions to require users to enter the URL starting with http:// and https://?
1. First of all, it is a simple layout, needless to say:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Unt titled document</title></head><body>The URL must start with http:// or https:// and must be a URL^_^! <br /><input type="text" id="url" /><button onclick="CheckUrl()">Confirm</button></body></html>
2. The second is the script, there is actually no need to say it. The key is the 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 is not a URL!"); } else{ alert("Input successful"); }}</script>In: var reg=/^([hH][tT]{2}[pP]:////|[hH][tT]{2}[pP][sS]:////)(([A-Za-z0-9-~]+)/.)+([A-Za-z0-9-~//])+$/;
1. In Javascript, since all variables are var, the regular expression must be written in two slashes, /.../, and then the slashes/ in the regular expression must be written as //
2. ^ means that it must start with..., [] means a test unit, that is, something that a certain character can accommodate. For example, ^([hH][tT]{2}[pP]:////|[hH][tT]{2}[pP][sS]:////), which means that it requires that it starts with http:// or https://. |Yes, the first character is h or H, the second and third characters are [tT], {2} means that including this character and the 1 character after it must be [tT], and so on
3. ([A-Za-z0-9-~]+) means that including this character and its subsequent characters must be uppercase, lowercase letters, numbers, minus signs-or ~
Character + means: match the characters before the + sign 1 or n times, for example: /a+/match 'a' in "candy" and all 'a' in "caaaaaaandy".
4. Therefore (([A-Za-z0-9-~]+)/.)+ means XXX. This thing ending with a dot must appear at least once before the character ([A-Za-z0-9-~//])+$.
5. $ means that it must end with uppercase letters, lowercase letters, numbers, minus signs -, ~, /
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.