① determine if a String conforms to a regular requirement
Patt.test (String);
② find a string that matches the regular substring
Patt.exec (String);
"Example" form submission:
A. User name cannot be empty, only numbers and letters, 6-11-bit
B.email cannot be empty and properly formatted
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <style>Table{width:400px;Height:400px;Border:0;Border-collapse:collapse;background:Orange; }TD{Border:1px solid Gray;cursor:Pointer; } </style></Head><Body> <formAction=""> <P>User name:<inputtype= "text"name= "username"ID=""></P> <P>Email:<inputtype= "text"name= "Email"ID=""></P> <P><inputtype= "Submit"value= "Submit"></P> </form></Body><Script>document.getElementsByTagName ("form")[0].onsubmit= function(){ varPatt= /^[a-za-z0-9]{6,11}$/; if(!patt.test (Document.getelementsbyname ("username")[0].value) {alert ("user name can only be 6-11 digits and letter composition"); return false; } Patt= /^[a-za-z0-9\.-][email protected]\w+ (\.\w+) +$/; //Domain name cannot have _, can have- if(!patt.test (Document.getelementsbyname ("Email")[0].value) {alert ("Mailbox Error"); return false; } }</Script></HTML>
Javascript notes and summaries (2-18) regular validation and regular matching