7_bootstap之綜合案例
阿新 • • 發佈:2018-12-06
13、綜合案例
13.1、案例需求
要求:頁面頂部的三部分在PC螢幕上顯示為一行,在移動裝置螢幕上顯示為一部分一行; 導航條在大螢幕展示全部內容,在移動裝置上需要將內容能夠摺疊/展開; 使用者名稱/密碼/確認密碼不能為空,密碼需和確認密碼一致,如果不符合,阻止註冊操作,並將錯誤資訊展示給使用者看。 onsubmit
13.2、需求分析
13.3、案例實現
<script> //密碼和確認密碼一致性校驗 //前提:密碼和確認密碼必須填寫 function checkPwdAndRepwd(f1,f2){if(f1&&f2){ //密碼和確認密碼不為空,進行非空校驗 //1、密碼和確認密碼 值拿到 var pwd=document.getElementById("password").value; var repwd=document.getElementById("repassword").value; var msg=document.getElementById("repasswordMsg"); var div=document.getElementById("repasswordDiv");//2、一致性判斷 if(pwd==repwd){ div.className="form-group"; msg.innerHTML=""; return true; }else{ div.className+=" has-error"; msg.innerHTML="必須和密碼一致"; return false; } }else{ //密碼和確認密碼有一個為空,直接返回false return false; } }//非空校驗 function checkNotNull(nid){ //1、獲取表單輸入項 元素物件 var nodex=document.getElementById(nid); //獲取對應的錯誤資訊回顯 label元素 var msg=document.getElementById(nid+"Msg"); //獲取對應的DIV var div=document.getElementById(nid+"Div"); //2、對進行非空判斷 var reg = /^\s*$/;//如果有0~多個空白符,就為true if(reg.test(nodex.value)){ //資訊不合格 div.className+=" has-error"; msg.innerHTML="不能為空"; return false; }else{ //資訊合格 div.className="form-group"; msg.innerHTML=""; return true; } } //表單校驗方法 function checkForm(){ //使用者名稱 var flag1=checkNotNull("username"); //密碼 var flag2=checkNotNull("password"); //確認密碼 var flag3=checkNotNull("repassword"); //一致性校驗 var flag4=checkPwdAndRepwd(flag2,flag3); return flag1&&flag2&&flag3&&flag4; } </script> </head> <body> <div class="container"> <!-- 網站頭部 --> <div class="row"> <div class="col-md-4"> <img src="../img/logo2.png" /> </div> <div class="col-md-4"> <img src="../img/header.png" /> </div> <div class="col-md-4"> <ul class="list-inline" style="margin-top:10px;"> <li><a href="" class="btn btn-primary">登入</a></li> <li><a href="" class="btn btn-primary">註冊</a></li> <li><a href="" class="btn btn-danger">購物車</a></li> </ul> </div> </div> <!-- 導航條 --> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">首頁</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">手機數碼<span class="sr-only">(current)</span></a></li> <li><a href="#">電腦辦公</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多
<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">母嬰用品</a></li> <li><a href="#">汽車配件</a></li> <li role="separator" class="divider"></li> </ul> </li> </ul> <form class="navbar-form navbar-right" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </nav> <!-- 註冊頁面主體--> <div class="row" style="background-image: url(../img/regist_bg.jpg)"> <div class="col-sm-8 col-sm-offset-2" style="border:5px solid gainsboro;"> <!-- 表單部分 --> <div class="row"> <div class="col-sm-8 col-sm-offset-2"> <font color="#204D74" size="4">會員註冊</font> </div> </div> <form class="form-horizontal" onsubmit="return checkForm()"> <div id="usernameDiv" class="form-group"> <label class="col-sm-2 control-label">使用者名稱</label> <div class="col-sm-8"> <input type="text" class="form-control" id="username" name="username" placeholder="請輸入使用者名稱"> </div> <label id="usernameMsg" class="col-sm-2 control-label"></label> </div> <div id="passwordDiv" class="form-group"> <label class="col-sm-2 control-label">密碼</label> <div class="col-sm-8"> <input type="password" class="form-control" id="password" name="password" placeholder="請輸入密碼"> </div> <label id="passwordMsg" class="col-sm-2 control-label"></label> </div> <div id="repasswordDiv" class="form-group"> <label class="col-sm-2 control-label">確認密碼</label> <div class="col-sm-8"> <input type="password" class="form-control" id="repassword" placeholder="請輸入確認密碼"> </div> <label id="repasswordMsg" class="col-sm-2 control-label"></label> </div> <div class="form-group"> <label class="col-sm-2 control-label">email</label> <div class="col-sm-8"> <input type="text" class="form-control" id="email" name="email" placeholder="請輸入email"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">姓名</label> <div class="col-sm-8"> <input type="text" class="form-control" id="name" name="name" placeholder="請輸入姓名"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">性別</label> <div class="col-sm-8"> <input type="radio" name="sex" value="man" checked="checked"/>男 <input type="radio" name="sex" value="woman"/>女 </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-danger btn-lg" value=" 注 冊 "/> </div> </div> </form> </div> </div> <!-- 網站底部 --> <div class="row"> <div class="col-xs-12"> <img src="../img/footer.jpg" width="100%"/> </div> </div> <div class="row"> <div class="col-xs-12 text-center"> <ul class="list-inline"> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> <li><a href="">聯絡我們</a></li> </ul> </div> </div> <div class="row"> <div class="col-xs-12 text-center"> Copyright © 2005-2020 淘寶商城 版權所有 </div> </div> </div> </body>