Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use regular expressions to check whether the input box to enter a URL     - Search Linux commands and files - which, whereis, locate, find (Linux)

- Some Linux networking tools you might not know (Linux)

- 10 example of the detection memory usage free Linux commands (Linux)

- ssh port forwarding Comments (Server)

- Depth Java Singleton (Programming)

- Nginx Keepalived Nginx monitoring scripts (Server)

- Linux scheduling summary (Linux)

- Articles do not resolve after opening under Ubuntu WordPress setting a fixed link (Server)

- Smooth upgrade to OpenSSH 6.7 Procedure (Linux)

- Iptables in Ubuntu (Linux)

- Modify Linux SSH default port 22 in several ways (Linux)

- Eclipse improve the efficiency of good habits (Programming)

- About Python default character set (Linux)

- Detailed Linux su command to switch users Mistakes (Linux)

- I use the desktop environment in GNU / Linux combination tool (Linux)

- Docker Private Registry Installation Guide at CentOS6.X (Linux)

- Teach you to diagnose problems with strace (Linux)

- Android components save state series - Activity (Programming)

- System Safety: Windows and Linux platforms (Linux)

- Cache implementation APP interacts with the server-side interface control Session (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:      
 
- GO five stages of language learning (Programming)
- Linux user groups, file permissions Detailed (Linux)
- Git commands (Linux)
- expdp reported ORA-39181 Export Processing Method (Database)
- Ubuntu install Scala 2.10.x version (Linux)
- Oracle Duplicate build DataGuard (Database)
- Eclipse remove all comments and code spaces (Linux)
- Linux (CentOS) directory file management and file system file compression packing (Linux)
- Ubuntu disable graphics card (Linux)
- MySQL Authorized a recovered (Database)
- SQLite database commonly used sentences and visualization tools on MAC MeasSQLlite use (Database)
- CentOS 7 install Hadoop-cdh-2.5 on Mesos (Server)
- C ++ runtime environment built on CentOS 6.6 and Oracle database connection (Database)
- Wireless LAN security solutions (Linux)
- Ubuntu and derivative users to install the system launcher SimDock 1.3 (Linux)
- Elasticsearch 2.20 Beginners: aggregation (Server)
- Can not remember how to solve the problem under Ubuntu brightness setting (Linux)
- Boot automatically remove Linux operating history records (Linux)
- Linux (Debian) install software, missing dynamic link libraries .so (Linux)
- Android Unzip the ZIP / GZIP data (based on the InflaterInputStream implementation) (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.