项目需求

表单内容为空时
image.png
邮箱格式验证
image.png
密码要求
image.png
验证通过的情况
image.png

基础功能实现, 直接上代码

HTML部分:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Form Validator</title>
  7. <link rel="stylesheet" href="style.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <form action="" id="form" class="form">
  12. <h2>注册</h2>
  13. <div class="form-control">
  14. <label for="username">姓名</label>
  15. <input type="text" id="username" placeholder="请输入用户名">
  16. <small>错误提示</small>
  17. </div>
  18. <div class="form-control">
  19. <label for="email">邮箱</label>
  20. <input type="text" id="email" placeholder="请输入邮箱">
  21. <small>错误提示</small>
  22. </div>
  23. <div class="form-control success">
  24. <label for="password">密码</label>
  25. <input type="password" id="password" placeholder="请输入密码">
  26. <small>错误提示</small>
  27. </div>
  28. <div class="form-control">
  29. <label for="password2">确认密码</label>
  30. <input type="password" id="password2" placeholder="请输入确认密码">
  31. <small>错误提示</small>
  32. </div>
  33. <button>提交</button>
  34. </form>
  35. </div>
  36. <script src="script.js"></script>
  37. </body>
  38. </html>

CSS部分:

  1. :root {
  2. --success-color: #2ecc71;
  3. --error-color: #e74c3c;
  4. }
  5. * {
  6. margin: 0;
  7. padding: 0;
  8. box-sizing: border-box;
  9. }
  10. body {
  11. background-color: #f9f6fb;
  12. font-family: Arial, Helvetica, sans-serif;
  13. display: flex;
  14. align-items: center;
  15. justify-content: center;
  16. min-height: 100vh;
  17. }
  18. .container {
  19. background-color: #fff;
  20. border-radius: 5px;
  21. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  22. }
  23. h2 {
  24. text-align: center;
  25. margin: 0 0 20px 0;
  26. }
  27. .form {
  28. padding: 30px 40px;
  29. }
  30. .form-control {
  31. margin-bottom: 20px;
  32. padding-bottom: 10px;
  33. position: relative;
  34. width: 400px;
  35. }
  36. .form-control label {
  37. color: #777;
  38. display: block;
  39. margin-bottom: 5px;
  40. }
  41. .form-control input {
  42. width: 100%;
  43. border: 2px solid #f0f0f0;
  44. border-radius: 4px;
  45. display: block;
  46. font-size: 14px;
  47. padding: 10px;
  48. }
  49. .form-control input:focus {
  50. border-color: #777;
  51. outline: 0;
  52. }
  53. .form-control.success input {
  54. border-color: var(--success-color);
  55. }
  56. .form-control.error input {
  57. border-color: var(--error-color);
  58. }
  59. .form-control small {
  60. color: var(--error-color);
  61. position: absolute;
  62. left: 0;
  63. bottom: -8px;
  64. visibility: hidden;
  65. }
  66. .form-control.error small {
  67. visibility: visible;
  68. }
  69. .form button {
  70. cursor: pointer;
  71. background-color: #3498db;
  72. border: 2px solid #3498db;
  73. border-radius: 4px;
  74. width: 100%;
  75. display: block;
  76. padding: 10px;
  77. margin-top: 20px;
  78. font-size: 16px;
  79. color: #fff;
  80. }

JS部分:

  1. //获取元素
  2. // const document = window.document
  3. const form = document.getElementById('form')
  4. const username = document.getElementById('username')
  5. const email = document.getElementById('email')
  6. const password = document.getElementById('password')
  7. const password2 = document.getElementById('password2')
  8. //输入处理函数
  9. function showError(input, message){
  10. const formControl = input.parentElement
  11. formControl.className = 'form-control error'
  12. const small = formControl.querySelector('small')
  13. small.innerText = message
  14. }
  15. function showSuccess(input){
  16. const formControl = input.parentElement
  17. formControl.className = 'form-control success'
  18. }
  19. //邮箱验证正则
  20. function isValidEmail(email){
  21. const re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  22. return re.test(String(email))
  23. }
  24. //添加事件监听
  25. form.addEventListener('submit', function(e){
  26. e.preventDefault()
  27. // console.log(username.value)
  28. if(username.value === ''){
  29. showError(username, '用户名为必填项')
  30. console.log(1)
  31. }else{
  32. showSuccess(username)
  33. }
  34. if(email.value === ''){
  35. showError(email, '邮箱为必填项')
  36. }else if (!isValidEmail(email.value)){
  37. showError(email, '邮箱格式错误')
  38. }else{
  39. showSuccess(email)
  40. }
  41. if(password.value === ''){
  42. showError(password, '密码为必填项')
  43. console.log(1)
  44. }else{
  45. showSuccess(password)
  46. console.log(2)
  47. }
  48. if(password2.value === ''){
  49. showError(password2, '确认密码为必填项')
  50. console.log(1)
  51. }else{
  52. showSuccess(password2)
  53. console.log(2)
  54. }
  55. })

重构

封装输入校验函数, 减少重复代码. 在此基础上, 让错误提示语有更多细节.

  1. //邮箱验证正则
  2. function isValidEmail(email){
  3. const re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  4. return re.test(String(email))
  5. }
  6. //提取控件关键字
  7. function getKeywords(input){
  8. return input.placeholder.slice(3)
  9. }
  10. //校验输入内容
  11. function checkRequired(inputArr){
  12. inputArr.forEach(function (input) {
  13. // console.log(input.value)
  14. if (input.value.trim() === '') { //用trim()方法去除字符串首尾空格
  15. showError(input, `${getKeywords(input)}为必填项`)
  16. } else {
  17. showSuccess(input)
  18. }
  19. })
  20. }
  21. //添加事件监听
  22. form.addEventListener('submit', function(e){
  23. e.preventDefault()
  24. checkRequired([username, email, password, password2])
  25. })

剩余功能实现

最后完善用户名与密码的长度要求, 封装邮箱校验函数, 完成密码匹配功能.

  1. //获取元素
  2. // const document = window.document
  3. const form = document.getElementById('form')
  4. const username = document.getElementById('username')
  5. const email = document.getElementById('email')
  6. const password = document.getElementById('password')
  7. const password2 = document.getElementById('password2')
  8. //结果显示函数
  9. function showError(input, message){
  10. const formControl = input.parentElement
  11. formControl.className = 'form-control error'
  12. const small = formControl.querySelector('small')
  13. small.innerText = message
  14. }
  15. function showSuccess(input){
  16. const formControl = input.parentElement
  17. formControl.className = 'form-control success'
  18. }
  19. //邮箱校验
  20. function checkEmail(input){
  21. const re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  22. // return re.test(String(email))
  23. // const result = re.test(String(email))
  24. if (re.test(input.value.trim())) {
  25. showSuccess(input)
  26. } else {
  27. showError(input, '邮箱格式错误')
  28. }
  29. }
  30. //提取控件关键字
  31. function getKeywords(input){
  32. return input.placeholder.slice(3)
  33. }
  34. //校验输入内容
  35. function checkRequired(inputArr){
  36. inputArr.forEach(function (input) {
  37. // console.log(input.value)
  38. if (input.value.trim() === '') { //用trim()方法去除字符串首尾空格
  39. showError(input, `${getKeywords(input)}为必填项`)
  40. } else {
  41. showSuccess(input)
  42. }
  43. })
  44. }
  45. //字符长度校验
  46. function checkLength(input, min, max) {
  47. if (input.value.length < min){
  48. showError(input, `${getKeywords(input)}至少${min}个字符`)
  49. } else if (input.value.length > max){
  50. showError(input, `${getKeywords(input)}至多${max}个字符`)
  51. } else {
  52. showSuccess(input)
  53. }
  54. }
  55. //验证密码匹配
  56. function checkPasswordMatch(input1, input2){
  57. if (input1.value !== input2.value){
  58. showError(input2, '密码不匹配')
  59. }
  60. }
  61. //添加事件监听
  62. form.addEventListener('submit', function(e){
  63. e.preventDefault()
  64. checkRequired([username, email, password, password2])
  65. checkLength(username, 3, 15)
  66. checkLength(password, 6, 12)
  67. checkEmail(email)
  68. checkPasswordMatch(password, password2)
  69. })

效果演示: 链接
项目地址: 链接