我想知道这个电子邮件验证功能是否正确制作,或者可以以不同的方式完成。我也想知道如何在不中断过渡的情况下删除错误内容,我尝试过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>
森栏
相关分类