Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use regular expressions to check whether the input box to enter a URL     - Docker improve safety (Server)

- Make command Detailed Tutorial (Programming)

- A deep understanding of Java enum (Programming)

- A brief description of Java 8 new features introduced syntax (Programming)

- Ordinary users how to use the firewall software (Linux)

- How to Install Winusb in Ubuntu 14.04 (Linux)

- CentOS 6.6 command-line automatic completion (Linux)

- Linux operating process information (Linux)

- Enable Intel Rapid Start in GNU / Linux (Linux)

- Oracle create user authorization and in PLSQL (Database)

- linux remote control software (Linux)

- Linux User Rights Study Notes (Linux)

- Why use Docker (Programming)

- Linux, Google Test (GTest) test environment to build step (Linux)

- Linux filtration empty file command summary (Linux)

- Linux memory management -free learning experience (Linux)

- Analysis of C ++ exception mechanism (Programming)

- Why JavaScript basic types can invoke methods (Programming)

- Linux cut Command Study Notes (Linux)

- RHEL7 Apache MPM configuration (Server)

 
         
  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:      
 
- Oracle rebuild index script (Database)
- Linux System Getting Started Learning: The Linux anacron command (Linux)
- 5 interesting Linux command line tips (Linux)
- Linux / Raspberry Pi using the following command-line based web browser (Linux)
- Install OpenGL development environment in Fedora and Ubuntu (Linux)
- Oracle set and remove columns unavailable (Database)
- SQL Server memory Misunderstanding (Database)
- About Leetcode on Binary Tree Algorithm summary (Programming)
- Java method to read and write files summary (Programming)
- Using Libreoffice under ubuntu (Linux)
- Linux system started to learn: how to solve the xxx is not in the sudoers file error (Linux)
- The most concise Systemd tutorial, just ten minutes (Linux)
- Swift defined type conversion and type aliases (typealias) (Programming)
- C # how to generate a folder or file automatically rename (Programming)
- Linux / Windows setup is complete port range (Linux)
- C language binary tree (Programming)
- Linux iostat command example explanation (Linux)
- Linux nice program origin of the name (Linux)
- Install VLC player on Ubuntu 14.10 (Linux)
- Repair CentOS 6.4 Grub boot (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.