如有如下註冊介面,請寫出相應的html和Script指令碼

來源:互聯網
上載者:User

標籤:dib   cee   ttl   nvm   isp   ptr   iam   vbo   vcl   

如有如下註冊介面,請寫出相應的html和Script指令碼

 

  

 

  我們經常會遇到這樣的需求,今天用原生js來實現。

   

  實現思路:

  1、布局,通過div層將 使用者名稱輸入,密碼輸入、確認密碼輸入,垂直排列

      使用者名稱、輸入框、提示分別用Label、input、span,

  要注意的是Label、input、span都是行級元素,需要轉換成區塊層級元素進行寬高設定。

  布局的圖:如下

  

      2.根據布局分層次設定布局的樣式

 

  3.JS裡實現button的點擊事件,通過JS,DOM操作擷取input的值,進行驗證。

 

  代碼實現:

  register.html:

  

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href="register.css" type="text/css">    <script type="text/javascript" src="register.js"></script></head><Script></Script><body class="body">    <div class="nav"   >        <div class="navitem">            <label class="label" >使用者名稱:</label>            <input  class="registerinput"  type="text" id="register_name"  />            <span class="register_error" id="register_name_error"  >請輸入字母和數字</span>        </div>        <div class="navitem" >            <label class="label"  >密碼:</label>            <input class="registerinput"  type="password" id="password" />            <span class="register_error" id="password_error"  >請輸入數字密碼</span>        </div>        <div class="navitem">            <label class="label" >確認密碼:</label>            <input class="registerinput" type="password" id="affirmpassword"   />            <span class="register_error" id="affirm_password_error"  >請輸入確認密碼</span>        </div>        <div class="navbtn" >            <Button class="myBtn"  id="myBtn" type="button" >註冊</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 () {//頁面載入後執行    document.getElementById("myBtn").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‘);        //重設驗證提示資訊        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_error.style.color="red";            return;        }        if(!regloginName.test(login_name)){            login_name_error.innerHTML="登入名稱必須是數字或字母";            login_name_error.style.color="red";            return;        }        if(password.length==0){            password_error.innerHTML="密碼不可為空";            password_error.style.color="red";            return;        }        if(affirmpassword.length==0){            affirm_password_error.innerHTML="確認密碼不可為空";            affirm_password_error.style.color="red";            return;        }        if(!regpassword.test(password)){            password_error.innerHTML="密碼請輸入數字";            password_error.style.color="red";            return;        }        if(password.length>6){            password_error.innerHTML="密碼不能超過6位";            password_error.style.color="red";            return;        }        if(password!==affirmpassword){            password_error.innerHTML="兩次密碼輸入必須相同";            password_error.style.color="red";            return;        }        alert(‘註冊成功‘);    }}

 

如有如下註冊介面,請寫出相應的html和Script指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.