Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use regular expressions to check whether the input box to enter a URL     - Service manager OpenBSD on rccl (Server)

- Linux, grep, sed usage (Linux)

- Ubuntu 15.10 15.04 14.10 14.04 Install Ubuntu Tweak (Linux)

- Ubuntu system safe and caution sudo su command (Linux)

- SSH port forwarding application (Server)

- RedHat Redis Linux installation (Database)

- Ubuntu 15.04 / 14.04 install Ubuntu After Install 2.6 (Linux)

- Introduction to Linux Shell (Programming)

- SQLite3 simple operation (Database)

- Comparison of one-time transaction and CTE insert data (Database)

- Java Performance Tuning the proxy mode (Programming)

- Linux System Getting Started Learning: DeVeDe installed on Linux to create a video DVD (Linux)

- ActiveMQ5.10.2 version configuration JMX (Linux)

- Java loop list to solve the problem of Joseph ring (Programming)

- Some MySQL interview questions (Database)

- CentOS 6.5 installation and deployment SVN 1.8.10 (Linux)

- Configuring Proxy on a Unix terminal, accelerate Android Studio Construction (Linux)

- CentOS yum source deployment (Linux)

- How to use the on-screen keyboard in Linux (Linux)

- Python 2 Chinese garbage problem solved (Linux)

 
         
  Use regular expressions to check whether the input box to enter a URL
     
  Add Date : 2017-02-28      
         
       
         
  This feature on the website is also very common, Links section, form-filling personal home page, when taken to verify the use of JavaScript to a URL.

This test does not write, it is best to use regular expressions to certification.

Things provides input can only be http: // and https: // at the beginning, and must be a URL.

Some people say, why like www.1.com this page to do so?

This is in case you get something the user to enter a structure when the hyperlink, a tag href attribute if never quite http: // or https: // something, it will be considered the root directory in your site back then write the URL address and then jump, everyone should know this. For example, < a href="www.1.com"> xxx < /a>, my URL is http: // localhost, then click on the display of a label is after xxx, simply jumps to http: // localhost / www.1.com this position, of course, wrong.

For example, the following text box, how to use regular expressions to do require user input must be http: // and https: // URLs beginning of it?

1, the first is a simple layout, it goes without saying:

< ! 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> Linux commune - Linux System Portal www.abctest.com < / title>
< / Head>

< Body>
URL must start with http: // or https: // at the beginning, and must be a site ^ _ ^! < br />
< Input type = "text" id = "url" />
< Button onclick = "CheckUrl ()"> OK < / button>
< / Body>
< / Html>

2, followed by the script, in fact, needless to say, is the key piece of a 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 is not in the URL http: // https:! // at the beginning of, or instead of a URL");
 }
 else {
  alert ( "Enter Success");
 }
}
< / Script>

in:

var reg = / ^ ([hH] [tT] {2} [pP]: \ / \ / | [hH] [tT] {2} [pP] [sS]: \ / \ /) (([A- Za-z0-9- ~] +) \) + ([A-Za-z0-9- ~ \ /]) + $ /.;

Among

1, Javascript being, since all variables are var, so the regular expression must be written in two slashes among / ... /, then the regular expression inside the slash / must be written \ /

2, expressed the need ...... ^ start with, [] represents a test unit, which is something of a character that can be accommodated, such as ^ ([hH] [tT] {2} [pP]: \ / \ / | [hH ] [tT] {2} [pP] [sS]: \ / \ /), is to ask with http: // or https: // at the beginning of the meaning. | Or is, the first character is h or H, second, three characters are [tT], {2} including this one behind the characters and their characters must be either [tT] mean, after so

3, ([A-Za-z0-9- ~] +) represented, including the character and subsequent characters must be uppercase letters, lowercase letters, numbers, minus sign - or -

Character + means: match the preceding character + No. 1 or n times, for example: / a + / matches the "candy" in the 'a' and "caaaaaaandy" all 'a'.

4, so (([A-Za-z0-9- ~] +) \.) + Indicates XXX. This points to the end of something, you must ([A-Za-z0-9- ~ \ /]) + this character appears in front of at least $ 1

5, $ express must be uppercase letters, lowercase letters, numbers, minus sign -, -, / end
     
         
       
         
  More:      
 
- Towards Docker (Server)
- Zend Studio PHP syntax color scheme to export (Linux)
- Upgrading Oracle 11.2.0.1 to 11.2.0.3 (Database)
- PHP loop reference caused strange problems (Programming)
- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)
- Install Open vSwitch under CentOS 6.5 (Linux)
- C ++ Learning Notes: references (Programming)
- Using a proxy method under Linux terminal (Linux)
- TWiki LDAP error appears the problem is solved (Linux)
- Linux kernel VLAN study notes (Programming)
- Ubuntu install OpenMPI (Linux)
- installation process of Matlab 2012a under Ubuntu 14.04 (Linux)
- Python control multi-process and multi-threaded concurrency (Programming)
- Upload the project to GitHub, synchronous remote repository Github (Linux)
- Linux / proc directory Comments (Linux)
- Installation of network monitoring ntopng under CentOS 6.4 (Linux)
- Linux network cut package is not fully defragment (Linux)
- MySQL5.7 implement virtual column expression index (Database)
- Linux - Common process the command (Linux)
- How to install with JSON support in Ubuntu 15.04 SQLite 3.9.1 (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.