觀察功能
製作一個會員申請的表單
輸入框的顏色變化
判斷條件的設置
任務拆解 抓出要互動的元素 1 2 3 4 5 const form = document .querySelector ('#form' );const username = document .querySelector ('#username' );const email = document .querySelector ('#email' );const password = document .querySelector ('#password' );const password2 = document .querySelector ('#password2' );
獲取物件層次中的父物件
針對指定元素取得或設置指定的class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function showError (input, message ) { const formControl = input.parentElement ; formControl.className = 'form-control error' ; } form.addEventListener ('submit' , function (e ) { e.preventDefault (); if (username.value === '' ) { showError (username, 'Username is required' ); } else { showSuccess (username); } })
1 2 const small = formControl.querySelector ('small' ); small.innerText = 'Username is required' ;
成功輸入 1 2 3 4 5 function showSuccess (input ) { const formControl = input.parentElement ; formControl.className = 'form-control success' ; }
複製相同條件判斷
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 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 { showSuccess (email); }; if (password.value === '' ) { showError (password, 'Password is required' ); } else { showSuccess (password); }; if (password2.value === '' ) { showError (password2, 'Password 2 is required' ); } else { showSuccess (password2); }; })
設定符合的驗證
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 function isValiEmail (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 ()); } if (email.value === '' ) { showError (email, 'Email is required' ); } else if (!isValiEmail (email.value )) { showError (email, 'Email is not valid' ); } else { showSuccess (email); };
整合簡化 =>forEach 1 2 3 4 5 6 7 8 9 10 11 function checkRequired (inputArr ) { inputArr.forEach (function (input ) { if (input.value .trim () === '' ) { showError (input, 'is required' ); } else { showSuccess (input); } }); }
1 2 3 if (input.value .trim () === '' ) { showError (input, `${input.id} is required` );
將getFieldName() function拉出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function checkRequired (inputArr ) { inputArr.forEach (function (input ) { if (input.value .trim () === '' ) { showError (input, `${getFieldName(input)} is required` ); } else { showSuccess (input); } }); } function getFieldName (input ) { return input.id .charAt (0 ).toUpperCase () + input.id .slice (1 ); }
長度檢查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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); } }
密碼檢查 1 2 3 4 5 6 function checkPasswordMatch (input1, input2 ) { if (input1.value !== input2.value ) { showError (input2, 'Password do not match' ) } }
參考資料:JavaScript初學:DOM常用屬性與方法