& lt; ! DOCTYPE html>
<meta charset="utf-8" & gt;Form validation & lt;/title>
<body>
<style type="text/CSS" & gt;
Span {color: red;
The font - size: 0.75 rem;
}
</style>Window. The onl oad=function () {
Var usernameError=document. GetElementById (" usernameError "). The innerText
Var passwordError=document. GetElementById (" passwordError "). The innerText
Var password2Error=document. GetElementById (" password2Error "). The innerText
Var emailError=document. GetElementById (" emailError "). The innerText
Var usernameElt=document. GetElementById (" username ")//get the user name box element
Var username=usernameElt. Value//get the user name
The username=username. The trim ()//remove the blank before and after the
Var regExp=/^ [A Za - z0-9] + $/
UsernameElt. Onblur=function () {
If (username) {
//user name is not empty
If (username. Length<=6 | | username. Length> 14) {//verify the user name length legitimacy
UsernameError="illegal user name length"//illegal user name length
}
Else if (regExp. Test (username) {
//user name format legal
The else {
UsernameError="username format error"
}
}
} else {
UsernameError="user name cannot be empty"
}
}
UsernameElt. An onfocus=function () {
If (username==="") {
The username=""//if the user name is whitespace, empty
}
UsernameError=""//clear error message
}
Var passwordElt=document. GetElementById (" password ")//get the password element
Var password=passwordElt. Value//access password
PasswordElt. Onblur=function () {//verification password
If (password=="") {
PasswordError="password cannot be empty"
{} else if (password==username)
PasswordError="with the user name password cannot repeat"
}
}
PasswordElt. An onfocus=function () {
PasswordError=""//clear error message
}
Var password2Elt=document. GetElementById (" password2)//obtain confirmation password box
Var password2=password2Elt. Value//get confirmation password
Password2Elt. Onblur=function () {
If (password==password2) {
//password
} else {
Password2Error="passwords don't match"
}
}
Password2Elt. An onfocus=function () {
Password2Error=""//clear error message
}
Var emailElt=document. GetElementById (" email "),//obtain email box
Var email=emailElt. Value//access email
Var regExp2=/^ \ w + (\ w +/- +.]) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) * $///email regular expression
EmailElt. Onblur=function () {
If (email==="") {
EmailError. The innerText="mail can't for empty"
} else if (regExp2. Test (email)) {
//email address legitimate
} else {
EmailError="email address illegal"
}
}
Document. The getElementById (" email "). An onfocus=function () {
EmailError=""//clear error message
}
Var register=document. GetElementById (" register ")
Register. The onclick=function () {
UsernameElt. Focus ()
UsernameElt. The blur ()
PasswordElt. Focus ()
PasswordElt. The blur ()
Password2Elt. Focus ()
Password2Elt. The blur ()
EmailElt. Focus ()
EmailElt. The blur ()
//trigger each text box
}
If ((usernameError=="" & amp; & PasswordError=="" & amp; & Password2Error=="") & amp; & EmailError="") {
Document. The getElementById (" Form "). The submit ()
//submission form
} else {
Alert (" the form content is wrong!" )
}
Var reset=document. GetElementById (" reset ")
Reset the onclick=function () {
The username=""
Password=""
Password2=""
Email=""
}
}
</script>The user name & lt; Input type="text" id="username"/& gt;
Password & lt; Input type="password" id="password"/& gt;
Confirm password & lt; Input type="password" id="password2"/& gt;
Email & lt; Input type="text" id="email"/& gt;