Regular is used to describe the character of rules, often used for form validation. Let me talk about the creation of regular and simple to use it!
/ / Is a regular expression identifier
create
- Create literal var reg = / a /;
- Constructor creates var reg = new RegExp ( "a");
Use: can not be directly used must comply with the method
- character:
- Regular:
-
Screened substring qualified
-
Alternatively substring qualifying
-
Verify compliance with the rules of character
. 1 var oUser = document.getElementById ( "User" ); 2 var opass = document.getElementById ( "Pass" ); . 3 var opass2 = document.getElementById ( "PASS2" ); . 4 var obtn = document.getElementById ( "BTN" ); . 5 . 6 // defined in advance for each input frame a success status: false for the failure, true success . 7 var U = P2 = P = to false ; . 8 . 9 // username only support Chinese, letters, numbers, "-" "_" combination, 4-20 characters 10 ouser.onblur = function () { . 11 var REG = / ^ [\ u2E80- \ u9FFF \ W -] {4,20} $ / ; 12 is IF (reg.test ( the this .Value)) { 13 is the this .nextElementSibling.innerHTML = "username successful" ; 14 U = to true ; 15 } the else { 16 the this .nextElementSibling.innerHTML = "user name only supports Chinese letters , number, "-", "_" in combination, 4-20 characters " ; . 17 U = to false ; 18 is } . 19 } 20 is 21 is // alphanumeric special characters, one type of weak. Two types of the three types of intensity 22 is opass.onblur = function () { 23 is // defined in advance for each type of status: No 0, 1 occurs 24 var A = B = C = 0 ; 25 // if there is digital A 26 is var AREG = / \ D / ; 27 IF (aReg.test ( the this .Value)) { 28 A =. 1 ; 29 } 30 // if appears letter B 31 is var Breg = / [the Z-zA-A] / ; 32 iF (bReg.test ( the this .Value)) { 33 is B =. 1 ; 34 is } 35 // whether special c occurred 36 var CREG = / [ \ W is] / ; 37 [ IF(cReg.test ( the this .Value)) { 38 is C =. 1 ; 39 } 40 // After the accumulation state, the sum of the determination, determines the difficulty of the type 41 is Switch (A + B + C) { 42 is Case . 1 : 43 is the this .nextElementSibling.innerHTML = "simple"; BREAK ; 44 is Case 2 : 45 the this .nextElementSibling.innerHTML = "general"; BREAK ; 46 is Case . 3 : 47 the this .nextElementSibling.innerHTML = "difficult"; BREAK ; 48 } 49 P = to true ; 50 51 is 52 is // first time password, the password is not repeated as long as the blank, are doing repeat of the password 53 is IF (opass2.value == "") return ; 54 is IF ( the this . === value opass2.value) { 55 opass2.nextElementSibling.innerHTML = "uniform" ; 56 is P2 = to true ; 57 is } the else { 58 opass2.nextElementSibling.innerHTML = "inconsistent" ; 59 P2 = to false ; 60 } 61 } 62 63 opass2.onblur = function(){ 64 if(this.value === opass.value){ 65 this.nextElementSibling.innerHTML = "一致"; 66 p2 = true; 67 }else{ 68 this.nextElementSibling.innerHTML = "不一致"; 69 p2 = false; 70 } 71 } 72 73 obtn.onclick = function() { 74 // time of filing, it is determined that all the state must be all input boxes to true 75 IF (U && && P P2) { 76 Alert ( "OK" ) 77 } the else { 78 // long as there is not a true, are determined individually, to find the real error 79 IF (! U) { 80 Alert ( "username error" ) 81 } 82 IF (! P) { 83 Alert ( "password error" ) 84 } 85 IF (! P2) { 86 Alert ( "repeat password error") 87 } 88 } 89 }
Before there is no regular time to do a lot of trouble form validation, with regular few lines of code can be judged whether or not, is not very good yet. Regular just how to write all right, but you know it is written, set rules, according to this rule could write the correct character oh. Many people will find it hard to regular, looks like gibberish, but if a little split open, not that hard. You can see more and more to write about, in the end feel for what. Like others speculated that one understands, but a knock on the error codes are the same. See more practice!