Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use regular expressions to check whether the input box to enter a URL     - The Linux role of each directory contents (Linux)

- MySQL Data Types (Database)

- Using RAID in Linux: Create a RAID 5 (Linux)

- The lambda expression Java8 (constructor references) (Programming)

- Use regular expressions to check whether the input box to enter a URL (Programming)

- The correct method to modify the maximum number of concurrent connections Tomcat 7 (Server)

- X security settings in Ubuntu (Linux)

- How to use SHA256 checksum files download (Linux)

- How to configure Proftpd server on Fedora 22 (Server)

- MySQL replication master and slave structure built (Database)

- How to use the beta / unstable version of the software in Debian library (Linux)

- Using monitoring tool dsniff (Linux)

- TL-WR703N to install OpenWrt process notes (Linux)

- Python, and / or (Programming)

- Linux iptables firewall settings (Linux)

- Ubuntu 14.04.1 LTS compile and install the new kernel (Linux)

- Install Mac OS X 10.9 systems VMware10 (Linux)

- PHP 7.0 Upgrade Notes (Linux)

- Create RAID 1 (mirroring) with two disks (Linux)

- MySQL various log summary (Database)

 
         
  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:      
 
- Ubuntu install snmp packets Unlinked OID in IPATM-IPMC-MIB: marsMIB (Linux)
- Android Delete project useless resource file (Programming)
- Production Environment MongoDB Fragmentation and Cluster Solution (Database)
- Use FFmpeg processing high quality GIF (Programming)
- Linux formatted partition error Could not stat / dev / sda No such file or directory Solution (Linux)
- Setting Derby as Linux / Windows running as a service from the start (Server)
- Java rewrite the hashcode method (Programming)
- Install the open source database PostgreSQL 9.4 and phpMyAdmin on Ubuntu (Database)
- Service Discovery: Zookeeper vs etcd vs Consul (Server)
- Use OpenSSL to generate a certificate (Linux)
- The lambda expression Java8 (constructor references) (Programming)
- Linux Kernel 4.2 Installation Instructions (Linux)
- Oracle create a temporary table space group (Database)
- git checkout generated in the use of temporary br (Linux)
- CentOS 6 compiling httpd-2.4.10 (Server)
- Zabbix configuration of SNMP (Server)
- Linux process group, session daemon (Linux)
- Redis logging system (Database)
- Linux find command detailing (Linux)
- Brief Linux commands (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.