JavaScript mailbox Verification-Regular validation

Source: Internet
Author: User

First, RegExp1.1 create RegExp Object
New RegExp ("required, Regular expression", "optional, matching pattern g,i,m")
1.2 Methods for RegExp objects

    • Test: Retrieves the specified value in the string, returning True or false.

    • EXEC: Retrieves the specified value in the string, returns the found value, and no null.

    • Complie: Used to change the regular expression, or to delete the matching pattern.

1.2.1 Test ()
var r1 = new RegExp (' World '); Console.log (R1.test (' Hello, world! ')); Trueconsole.log (R1.test (' Hello, world! ')); Falsevar r2 = new RegExp (' World ', ' I '); Case insensitivity Console.log (r2.test (' Hello, world! ')); Truevar R3 = new RegExp (/world/i); Shorthand Console.log (r3.test (' Hello, world! ')); True
1.2.2 Exec ()
var r1 = new RegExp (' World '); Console.log (R1.exec (' Hello, world! ')); [' World ']console.log (r1.exec (' Hello, world! ')); Nullvar r2 = new RegExp (' World ', ' I '); Case insensitivity Console.log (r2.exec (' Hello, world! ')); [' World ']var r3 = new RegExp (/world/i); Shorthand Console.log (r3.exec (' Hello, world! ')); [' world ']var r4 = new RegExp (' O '); Console.log (R4.exec (' Hello, world! ')); [' o ']var r5 = new RegExp (' O ', ' gi '); Console.log (R5.exec (' Hello, world! ')); [' O ']console.log (r5.lastindex); 5 matches the position of the first character of the text, O is 4, and the next position is 5console.log (r5.exec (' Hello, world! ')); [' O '] matches the first o after the call continues to match Console.log (R5.lastindex); 9console.log (R5.exec (' Hello, world! ')); A null match is not returned Nullconsole.log (R5.lastindex); 0 lastindex Reset to 0
1.2.3 Complie ()
var r1 = new RegExp (' World '); Console.log (R1.exec (' Hello, world! ')); [' World ']r1.compile (' o '); Console.log (R1.exec (' Hello, world! ')); [' O ']r1.compile (' m '); Console.log (R1.exec (' Hello, world! ')); Nullvar r2 = new RegExp (' World '); Console.log (R2.test (' Hello, world! ')); Truer2.compile (' Mazey '); Console.log (R2.test (' Hello, world! ')); False
Second, the regular expression

    • ^$: Represents the start and end of a matching value.

    • +:1+, one or more.

    • *:0+, 0 or more.

    • ?: 0 /1, 0 or one.

    • {1,2}:1

      <=length<=2, length. < li= "" >
    • (): Represents a group of expressions.

    • []: matching the range of characters, I understand as a block, a lot of blocks placed in a group () inside.

Iii. examples
<form action= "" > Input: <input type= "text"  name= "Mazey"  id= "Mazey"  placeholder= " Please enter the mailbox "><input type=" button " value=" to verify " onclick=" Check (); " ></form><script>function check () {    var reg = new  regexp ("^[a-z0-9]+ ([. _\\-]*[a-z0-9]) *@ ([a-z0-9]+[-a-z0-9]*[a-z0-9]+.) {1,63} [a-z0-9]+$];  //Regular expression     var obj = document.getelementbyid ("Mazey");  //object to validate     if (obj.value ===  "") { //input cannot be empty          alert ("Input cannot be empty!");         return false;    }else if (! Reg.test (Obj.value)) { //Regular validation does not pass, the format is not         alert ("Verification does not pass!");         return false;    }else{         alert ("Pass!") "); &nbsP;       return true;    }}</script> 

JavaScript mailbox Verification-Regular validation

This article from "Do not know not to ask" blog, please be sure to keep this source http://mazey.blog.51cto.com/12997993/1948712

JavaScript mailbox Verification-Regular validation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.