Javascript Regular Expression and registration verification

Source: Internet
Author: User


A regular expression is a logical formula for string operations. It uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings. Regular Expressions are widely used. The following describes the regular expressions in javascript:

Regular Expression Syntax:

Syntax 1

Re =/pattern/[flags]

Syntax 2

Re = new Regexp ("pattern", ["Flags"])

Parameters

Re

Required. Name of the variable to be assigned a value in the regular expression mode.

Pattern

Required. The regular expression mode to use. If Syntax 1 is used, use the "/" character separation mode. If syntax 2 is used, quotation marks are used to mark the format.

Flags

Optional. If syntax 2 is used, the flag is enclosed in quotation marks. The flag can be used in combination and available include:

· G (all pattern in full-text search). For example, if you have hundreds of lines of strings to search, you need to add the g id (by default, the JS search range is one line)

· I (Case Insensitive)

· M (multi-line search). By default, JS finds the first matching string to complete any

Example:

Regexp. js

function match() {   str1 = "this is JAVA java Java , this is a first RegExp programe!";    //str2 = "java";    re = /java/i;    re2 = /java/g;    re3 = new RegExp("java", "i");    r = str1.match(re);    r2 = str1.match(re2);    r3 = str1.match(re3);    document.writeln(r);    document.writeln(r2);     document.writeln(r3);}

Test.html

Register and verify register. js
Function checkusername () {var username = document. getelementsbyname ("username") [0]. value; // Re = new Regexp ("\ s *", "G"); // Re =/(\ s *)/g // var isempty = Re. test (usrname); var namewarning = ""; if (! (New Regexp ("\ s +"). Test (username) {// determine whether the user name is empty // alert ("Enter the user name! "); Namewarning =" Enter the user name! "; Document. getelementbyid ("namechick "). innertext = namewarning; return false;} document. getelementbyid ("namechick "). innertext = namewarning; return true;} function checkpassword () {var pwdwarning = ""; var Password = document. getelementsbyname ("password") [0]. value; If (password! = ""&&! (New Regexp ("^ (\ s *) $"). Test (password) {// The password is not blank if (! /\ W {6 }/. test (password) {// The password is not a 6-digit character or number pwdwarning = "6-digit character or number ";}} else {// The password is empty pwdwarning = "enter the password! "; Document. getelementbyid ("pwdchick "). innertext = pwdwarning; return false;} document. getelementbyid ("pwdchick "). innertext = pwdwarning; return true;} function checkemail () {var emailwarning = ""; var email = document. getelementsbyname ("email") [0]. value; If (! (New Regexp ("[\ W [. _] + @ [\ W [. _] + \\. [\ W] + ")). test (email) {// The password is not a 6-digit character or number emailwarning = "Incorrect email format! "; // Return false;} document. getelementbyid (" emailchick "). innertext = emailwarning; return true ;}

Register.html

<HTML> 

Success.html

<HTML> 

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.