亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我在這個驗證功能上做得對嗎?

我在這個驗證功能上做得對嗎?

人到中年有點甜 2023-05-19 16:19:34
我想知道這個電子郵件驗證功能是否正確制作,或者可以以不同的方式完成。我也想知道如何在不中斷過渡的情況下刪除錯誤內容,我嘗試過innerHTML = ""但過渡停止工作。提前致謝。const email = document.querySelector('#email');eventListeners();function eventListeners() {  email.addEventListener('keyup', validateEmail);}function validateEmail() {  const email = document.querySelector('.email'),    error = document.querySelector('.error'),    inputEmail = document.querySelector('#email'),    formatEmail = /^(([^<>()\[\]\\.,;:\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 (inputEmail.value.match(formatEmail)) {    error.classList.remove("show");  } else {    if (inputEmail.value !== "") {      error.innerHTML = `<p>error</p>`;      error.classList.add("show")    } else {      error.classList.remove("show");    }  }}.error {  width: 200px;  max-height: 0;  transition: max-height 1s ease-out;  overflow: hidden;  background: #d5d5d5;  text-align: center;}.show {  max-height: 100px;  transition: max-height 1s ease-in;}<div class="name">  <label for="name">Name:</label>  <input type="text" id="name" name="name" placeholder="Name"></div><div class="email">  <label for="email">Email:</label>  <input type="email" id="email" name="email" placeholder="Email">  <div class="error"></div></div><div class="password">  <label for="password">Password:</label>  <input type="password" id="password" name="password" placeholder="Password"></div>
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

我覺得還可以,有幾點:

  1. 或事件更適合監聽輸入input。如果您想對輸入的每次更改做出反應,例如擊鍵,或從瀏覽器保存的電子郵件地址列表中選擇電子郵件,change請使用。input如果change 您只希望處理程序在用戶提交更改時運行,例如通過失去字段焦點。

  2. 要在隱藏時為錯誤元素設置動畫,您需要偵聽該transitionend事件,并且僅在該事件觸發后才刪除內容。這使動畫有時間完成。否則,盒子會立即坍塌,沒有任何動畫。

const email = document.querySelector('#email');


eventListeners();


function eventListeners() {

  email.addEventListener('input', validateEmail);

}


function validateEmail() {


  const email = document.querySelector('.email'),

    error = document.querySelector('.error'),

    inputEmail = document.querySelector('#email'),

    formatEmail = /^(([^<>()\[\]\\.,;:\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 (inputEmail.value.match(formatEmail)) {

    error.classList.remove("show");

  } else {

    if (inputEmail.value !== "") {

      error.innerHTML = `<p>error</p>`;

      error.classList.add("show")

    } else {

      error.addEventListener('transitionend', () => {

        error.innerHTML = '';

      }, {

        once: true

      });

      error.classList.remove("show");

    }

  }

}

.error {

  width: 200px;

  max-height: 0;

  transition: max-height 1s linear;

  overflow: hidden;

  background: #d5d5d5;

  text-align: center;

}


.error.show {

  max-height: 100px;

}

<div class="name">

  <label for="name">Name:</label>

  <input type="text" id="name" name="name" placeholder="Name">

</div>

<div class="email">

  <label for="email">Email:</label>

  <input type="email" id="email" name="email" placeholder="Email">

  <div class="error"></div>

</div>

<div class="password">

  <label for="password">Password:</label>

  <input type="password" id="password" name="password" placeholder="Password">

</div>


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號