在論壇等系統的用戶注冊功能中,如果用戶忘記填寫必填信息,如用戶名、密碼等,瀏覽器會彈出警告框,提示用戶當前有未填信息。
這個典型的應用就是通過JavaScript實現的。如圖所示是一個簡單的用戶注冊頁面:
按要求用戶必須輸入用戶名、密碼和確認密碼,而且兩次輸入的密碼必須相同,否則,系統會提示錯誤。
當用戶沒有輸入用戶名就提交注冊時,系統會彈出如下圖所示的警告窗口,強制用戶必須輸入用戶名。


簡單的做法:
1 <script language="JavaScript"> 2 function check(){ 3 if(document.form1.username.value==""){ 4 alert("用戶名不能為空!"); 5 document.form1.username.value.focus(); 6 return false; 7 } 8 if(document.form1.password.value==""){ 9 alert("密碼不能為空!"); 10 document.form1.password.value.focus(); 11 return false; 12 } 13 if(document.form1.password.value.length<6) 14 { 15 alert("密碼長度不能少於6位"); 16 document.form1.password.value.focus(); 17 return false; 18 } 19 else 20 if(document.form1.repassword.value!=document.form1.password.value){ 21 alert("兩次輸入的密碼不一致!"); 22 document.form1.repassword.focus(); 23 return false; 24 }} 25 </script>
