If you have the following registration interface, please write the appropriate HTML and script scripts
We often encounter such a demand, today with native JS to achieve.
Implementation ideas:
1, layout, through the div layer user name input, password input, confirm password input, vertical arrangement
User names, input boxes, and prompts, respectively, with a label, enter, Span,
Note that label, input, and span are row-level elements that need to be converted to block-level elements for wide-height settings.
Layout diagram: The following
2. Style The layout hierarchically based on layout
The Click event of the button is implemented in 3.JS, and the value of input is obtained by js,dom operation for verification.
Code implementation:
Register.html:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Linkrel= "stylesheet"href= "Register.css"type= "Text/css"> <Scripttype= "Text/javascript"src= "Register.js"></Script></Head><Script></Script><Bodyclass= "Body"> <Divclass= "NAV" > <Divclass= "Navitem"> <labelclass= "Label" >User name:</label> <inputclass= "Registerinput"type= "text"ID= "Register_name" /> <spanclass= "Register_error"ID= "Register_name_error" >Please enter letters and numbers</span> </Div> <Divclass= "Navitem" > <labelclass= "Label" >Password:</label> <inputclass= "Registerinput"type= "Password"ID= "Password" /> <spanclass= "Register_error"ID= "Password_error" >Please enter a digital password</span> </Div> <Divclass= "Navitem"> <labelclass= "Label" >Confirm Password:</label> <inputclass= "Registerinput"type= "Password"ID= "Affirmpassword" /> <spanclass= "Register_error"ID= "Affirm_password_error" >Please enter a confirmation password</span> </Div> <Divclass= "Navbtn" > <Buttonclass= "Mybtn"ID= "Mybtn"type= "button" >Registered</Button> </Div> </Div></Body></HTML>
REGISTER.CSS:
. body{background-color:azure}.nav { text-align:center;margin:180px 500px 60px 500px; border:1px;}. navitem{margin:20px 0;}. Label{display:inline-block; width:100px; line-height:44px; Color: #999; Text-align:left; }.registerinput{display:inline-block;width:200px;text-align:left; box-sizing:border-box; padding:10px 0; Border:0;background-color: #ededed}.register_error{display:inline-block; width:200px;text-align:left;margin-left : 10px;}. navbtn{margin-top:60px}.mybtn{width:200px;height:40px; Background-color: #ededed; border:0}
Register.js:
/** * Created by Administrator on 2017/3/31. */window.onload=function () {//page is loaded after the document.getElementById ("mybtn") is executed. Onclick=function () {changetext ()}; function Changetext () {var regpassword = new RegExp ("^[0-9]*$"); var regloginname = new RegExp ("^[0-9a-za-z]*$"); var Login_name=document.getelementbyid (' Register_name '). Value; var Password=document.getelementbyid (' password '). value; var Affirmpassword=document.getelementbyid (' Affirmpassword '). Value; var Login_name_error=document.getelementbyid (' Register_name_error '); var Password_error=document.getelementbyid (' Password_error '); var Affirm_password_error=document.getelementbyid (' Affirm_password_error '); Reset the authentication prompt message login_name_error.innerhtml= ""; Login_name_error.style.color= "BLACK" password_error.innerhtml= ""; Password_error.style.color= "BLACK" affirm_password_error.innerhtml= ""; Affirm_password_error.style.color= "BLACK" Console.log (login_name); Console.log (password); Console.log (Affirmpassword); if (login_name.length==0) {login_name_error.innerhtml= "login name cannot be empty"; Login_name_error.style.color= "Red"; Return } if (!regloginname.test (login_name)) {login_name_error.innerhtml= "login must be a number or letter"; Login_name_error.style.color= "Red"; Return } if (password.length==0) {password_error.innerhtml= "Password cannot be empty"; Password_error.style.color= "Red"; Return } if (affirmpassword.length==0) {affirm_password_error.innerhtml= "Confirm password cannot be empty"; Affirm_password_error.style.color= "Red"; Return } if (!regpassword.test (password)) {password_error.innerhtml= "password please enter a number"; Password_error.style.color= "Red"; Return } if (password.length>6) {password_error.innerhtml= "password cannot exceed 6 bits"; PaSsword_error.style.color= "Red"; Return } if (Password!==affirmpassword) {password_error.innerhtml= "two times password input must be the same"; Password_error.style.color= "Red"; Return } alert (' Registered success '); }}
If you have the following registration interface, please write the appropriate HTML and script scripts