javascript - 如何使用 javascript 验证选择标签?
问题描述
我正在构建一个动画表单,到目前为止,我只能验证输入标签并使其与我的表单动画一起使用,但我还需要在表单中使用选择标签,但我无法做到与动画一起工作,请帮我一些建议。谢谢!
https://codepen.io/andrewrico/pen/gyEGaw
function animatedForm() {
const arrows = document.querySelectorAll(".fa-arrow-down");
arrows.forEach(arrow => {
arrow.addEventListener("click", () => {
const input = arrow.previousElementSibling;
const parent = arrow.parentElement;
const nextForm = parent.nextElementSibling;
if (input.type === "text" && validateUser(input)) {
nextSlide(parent, nextForm);
} else if (input.type === "email" && validateEmail(input)) {
nextSlide(parent, nextForm);
} else if (input.type === "password" && validateUser(input)) {
nextSlide(parent, nextForm);
} else {
parent.style.animation = "shake 0.5s ease";
}
parent.addEventListener("animationend", () => {
parent.style.animation = "";
});
});
});
}
function validateUser(user) {
if (user.value.length < 6) {
console.log("not enough characters");
error("rgb(189, 87, 87)");
return false;
} else {
error("rgb(101, 109, 255)");
return true;
}
}
function validateEmail(email) {
const validation = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (validation.test(email.value)) {
error("rgb(101, 109, 255)");
return true;
} else {
error("rgb(189, 87, 87)");
return false;
}
}
function nextSlide(parent, nextForm) {
parent.classList.add('innactive');
parent.classList.remove('active');
nextForm.classList.add('active');
}
function error(color) {
document.body.style.background = color;
}
animatedForm();
选择标签应该能够通过验证才能传递到下一个问题。
解决方案
您的 JS 代码似乎是面向输入的,您只需在animatedForm()中的 else 中输入,您必须创建一个特殊情况供您选择以测试所选值。
推荐阅读
- c# - 尝试将图像加载到位图/图片框 Visual Studio 2019 时出现问题
- optaplanner - 使用 OptaPlanner 解决客户优先考虑的 VRP:高、标准或低
- ios - 如果长度小于规定长度,如何向组合字符串添加更多字符串?
- python - 使用 Tweepy 发布视频
- typescript - 如何使用异步等待处理 TypeScript 中的 Promise 异常?
- ios - -[AVPlayerLayer setDisplayEngineDelegate:]:无法识别的选择器发送到实例 0x2822fd6a0
- csv - 安全中断 csv 读写
- vim - 在 vim 中使用 clang-format.py 时发生错误
- r - 如何在 AWS EMR 版本 5.29 中安装 RcppArmadillo
- python - 如何捕捉 SimpleITK 警告?