成品:
成品網址
成品功能: 1.驗證輸入內容是否符合要求(有各種不同的驗證內容) 2.submit 按下去後會送出內容 3.驗證成功顯示綠色失敗顯示紅色
HTML 表格製作需要注意些一些 type 的地方: password-password email-text username-text 上 CSS 之前的 HTML:
程式碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <body > <div class ="container" > <form id ="form" class ="form" > <h2 > Register With Us</h2 > <div class ="form-control" > <label for ="username" > Username</label > <input type ="text" id ="username" placeholder ="Enter username" /> <small > Error message</small > </div > <div class ="form-control" > <label for ="Email" > Email</label > <input type ="text" id ="Email" placeholder ="Enter Email" /> <small > Error message</small > </div > <div class ="form-control" > <label for ="Password" > Password</label > <input type ="password" id ="Password" placeholder ="Enter Password" /> <small > Error message</small > </div > <div class ="form-control" > <label for ="password2" > Confirm Password</label > <input type ="password" id ="password2" placeholder ="Enter password again" /> <small > Error message</small > </div > <button type ="submit" > Submit</button > </form > </div > <script src ="script.js" > </script > </body >
CSS: 字體輸出 引入字體的部分要先點選你要的字體粗細大小後,點擊右上角綠色框框會跳出右邊視窗告訴你選了那些字體大小的選項後,點選下方紅框框處的@import 輸出成網址就可以使用瞜!
CSS 設置好的樣式
程式碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 @import url('https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,300;1,400;1,600;1,700;1,800&display=swap' );* { box-sizing : border-box; } body { background-color : #f9fafb ; font-family : 'Open Sans' , sans-serif; display : flex; align-items : center; justify-content : center; min-height : 100vh ; margin : 0 ; } .container { background-color : #fff ; border-radius : 5px ; box-shadow : 0 2px 10px rgba (0 , 0 , 0 , 0.3 ); width : 400px ; } h2 { text-align : center; margin : 0 0 20px ; } .form { padding : 30px 40px ; } .form-control { margin-bottom : 10px ; padding-bottom : 20px ; position : relative; } .form-control label { color : #777 ; display : block; margin-bottom : 5px ; } .form-control input { border : 2px solid #f0f0f0 ; border-radius : 4px ; width : 100% ; padding : 10px ; font-size : 14px ; } .form-control input :focus { outline : 0 ; border-color : #777 ; } .form-control .success input { border-color : #2ecc71 ; } .form-control .error input { border-color : #e74c3c ; } .form-control small { color : #e74c3c ; position : absolute; bottom : 0 ; left : 0 ; visibility : hidden; } .form-control .error small { visibility : visible; } .form button { cursor : pointer; background-color : #3498db ; border : 2px solid #3498db ; border-radius : 4px ; color : #fff ; display : block; font-size : 16px ; padding : 10px ; margin-top : 20px ; width : 100% ; }
小補充: CSS 教學-關於 display:inline、block、inline-block 的差別
JS: 作者展示簡單的驗證使用 if…else 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 const form = document .getElementById('form' );const username = document .getElementById('username' );const email = document .getElementById('email' );const password = document .getElementById('password' );const password2 = document .getElementById('password2' );function showError (input, message ) { const formControl = input.parentElement; formControl.className = 'form-control error' ; const small = formControl.querySelector('small' ); small.innerText = message; } function showSuccess (input ) { const formControl = input.parentElement; formControl.className = 'form-control success' ; } function isValidEmail (email ) { const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ; return re.test(String (email).toLowerCase()); }; form.addEventListener('submit' , function (e ) { e.preventDefault(); if (username.value === '' ) { showError(username, 'Username is required' ); } else { showSuccess(username); } if (email.value === '' ) { showError(email, 'Email is required' ); } else if (!isValidEmail(email.value)) { showError(email, 'Email is not valid' ); } else { showSuccess(email); } if (password.value === '' ) { showError(password, 'password is required' ); } else { showSuccess(password); } if (password2.value === '' ) { showError(password2, 'Password2 is required' ); } else { showSuccess(password2); } });
程式碼重構 & 並使用 function 簡化 if…else 條件式 因為事件監聽的部分太多 if..else 的使用程式碼太亂簡化成一個 funciton 加上一些 ES6 的語法來操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 function checkRequired (inputArr ) { inputArr.forEach(function (input ) { if (input.value.trim() === '' ) { console .log(input.id); showError(input, `${getFieldName(input)} is required` ); } else { showSuccess(input); } }) } function getFieldName (input ) { return input.id.charAt(0 ).toUpperCase() + input.id.slice(1 ); } form.addEventListener('submit' , function (e ) { e.preventDefault(); checkRequired([username, email, password, password2]); });
補充:charAt()
從一個字符串中返回指定的字符。
slice()
原為陣列選擇之起始值至終點值(不含終點值)部分, 但是如果不輸入終點值的部分的話則擷取起始值後方全部。
(完成版)確認內容長短 以及 Email & Password 是否吻合(完成版) 最後加入一些功能:
確認 password 以及 username 的長短 email 部分的判斷式修改 確認 password 是否跟 password2 相符合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 const form = document .getElementById('form' );const username = document .getElementById('username' );const email = document .getElementById('email' );const password = document .getElementById('password' );const password2 = document .getElementById('password2' );function showError (input, message ) { const formControl = input.parentElement; formControl.className = 'form-control error' ; const small = formControl.querySelector('small' ); small.innerText = message; } function showSuccess (input ) { const formControl = input.parentElement; formControl.className = 'form-control success' ; } function checkEmail (input ) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ; if (re.test(input.value.trim())) { showSuccess(input); } else { showError(input, 'Email is not valid' ); } } function checkRequired (inputArr ) { inputArr.forEach(function (input ) { if (input.value.trim() === '' ) { console .log(input.id); showError(input, `${getFieldName(input)} is required` ); } else { showSuccess(input); } }) } function checkLength (input, min, max ) { if (input.value.length < min) { showError(input, `${getFieldName(input)} must be at least ${min} characters` ) } else if (input.value.length > max) { showError(input, `${getFieldName(input)} must be less than${max} characters` ) } else { showSuccess(input); } } function checkPasswordMatch (input1, input2 ) { if (input1.value !== input2.value) { showError(input2, "Password do not match" ); } else { showSuccess(input2); } } function getFieldName (input ) { return input.id.charAt(0 ).toUpperCase() + input.id.slice(1 ); } form.addEventListener('submit' , function (e ) { e.preventDefault(); checkRequired([username, email, password, password2]); checkLength(username, 3 , 15 ); checkLength(password, 6 , 25 ); checkEmail(email); checkPasswordMatch(password, password2); });
補充: trim()
可以消除字首字尾的空白字元,可以減少 bug 產生
進階練習 題目: 優化使用者體驗,讓其輸入當下就判別是否符合條件不需要等到使用 submit
解法
1.使用 keyup 事件當輸入鍵盤當下就會觸發事件
2.把 keyup 事件分開並且把判別的 function 分開,這樣一次記只會處理一個視窗而不是全部搂!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 username.addEventListener("keyup" , function (e ) { e.preventDefault(); checkLength(username, 3 , 15 ); }); email.addEventListener("keyup" , function (e ) { e.preventDefault(); checkEmail(email); }); password.addEventListener("keyup" , function (e ) { e.preventDefault(); checkLength(password, 8 , 16 ); }); password2.addEventListener("keyup" , function (e ) { e.preventDefault(); checkPasswordMatch(password, password2); });