将 Keyup 添加到 JS 验证表单
Add Keyup to JS validation form
在我的简单表单中,我使用了这个简单的客户端验证。
当我按下 SUBMIT(更改样式输入和表单跨度)时,验证开始。
即使用户没有通过 SUBMIT 就填写了字段,我如何验证输入?
风格
<style>
.msc-login-form-input {
display: flex;
}
.msc-login-form-input.success > input {
color: #3F4254;
background-color: #ffffff;
}
.msc-login-form-input.errore > input {
background-color: #4d40ff;
color: #ffffff;
}
.msc-login-form-input.errore > input::-webkit-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input:-ms-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input::placeholder {
color: #ffffff;
}
.msc-login-form-input > span {
width: 35px;
background-color: rgba(0,0,0,0.05);
min-height: 100%;
align-items: center;
justify-content: center;
text-align: center;
display: flex;
}
.msc-login-form-input > span::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f100";
}
.msc-login-form-input.success > span::before {
content: "\f00c";
color:#FF1493;
}
.msc-login-form-input.errore > span::before {
content: "\f00d";
color:#4d40ff;
}
</style>
HTML 和 JS
此脚本检查表单的内部元素。如果,当我点击 SUBMIT 时字段为空,那么它会突出显示具有不同样式的输入并在 SPANs 标签中加载不同的图标。
<form id="signinform" method="post" action="#" class="wp-user-form" autocomplete="off">
<div class="msc-login-form-input">
<input type="text" name="log" value="" id="user_login" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input">
<input type="password" name="pwd" value="" id="user_pass" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input-sendh">
<input type="submit" id="submit-login" name="submit-login" value="Submit" class="user-submit" />
</div>
<script>
// ___________________________________________________________________
// validate contact form
const myform = document.getElementById('signinform');
myform.noValidate = true;
// custom form validation
myform.addEventListener('submit', validateForm);
// stop submission of valid form for demo
myform.addEventListener('submit', e => {
e.preventDefault();
const fd = new FormData(e.target);
for (const [name, value] of fd.entries()) {
console.log(name + ': ' + value);
}
});
// form validation
function validateForm(e) {
const
form = e.target,
field = Array.from(form.elements);
// reset fields
field.forEach(i => {
i.parentElement.classList.remove('errore');
i.parentElement.classList.add('success');
});
if (!form.checkValidity()) {
// form is invalid - cancel submit
e.preventDefault();
e.stopImmediatePropagation();
// apply invalid class
field.forEach(i => {
if (!i.checkValidity()) {
// field is invalid - add class
i.parentElement.classList.add('errore');
i.parentElement.classList.remove('success');
}
});
}
}
</script>
</form>
谢谢
根据您的评论。您可以添加侦听 Keydown(或 Keyup)的事件侦听器,然后删除显示错误的 class,而不是再次进行 运行 验证。
const myform = document.getElementById("signinform");
myform.noValidate = true;
// custom form validation
myform.addEventListener("submit", validateForm);
// stop submission of valid form for demo
myform.addEventListener("submit", (e) => {
e.preventDefault();
const fd = new FormData(e.target);
for (const [name, value] of fd.entries()) {
console.log(name + ": " + value);
}
});
// form validation
function validateForm(e) {
const form = e.target,
field = Array.from(form.elements);
// reset fields
field.forEach((i) => {
i.parentElement.classList.remove("errore");
i.parentElement.classList.add("success");
});
if (!form.checkValidity()) {
// form is invalid - cancel submit
e.preventDefault();
e.stopImmediatePropagation();
// apply invalid class
field.forEach((i) => {
if (!i.checkValidity()) {
// field is invalid - add class
i.parentElement.classList.add("errore");
i.parentElement.classList.remove("success");
}
});
}
}
// remove the error class on Keydown input
const formInputs = document.querySelectorAll(".msc-login-form-input");
formInputs.forEach((input) => {
input.addEventListener("keydown", () => {
input.classList.remove("errore");
input.classList.add("success");
});
});
.msc-login-form-input {
display: flex;
}
.msc-login-form-input.success > input {
color: #3f4254;
background-color: #ffffff;
}
.msc-login-form-input.errore > input {
background-color: #4d40ff;
color: #ffffff;
}
.msc-login-form-input.errore > input::-webkit-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input:-ms-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input::placeholder {
color: #ffffff;
}
.msc-login-form-input > span {
width: 35px;
background-color: rgba(0, 0, 0, 0.05);
min-height: 100%;
align-items: center;
justify-content: center;
text-align: center;
display: flex;
}
.msc-login-form-input > span::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f100";
}
.msc-login-form-input.success > span::before {
content: "\f00c";
color: #ff1493;
}
.msc-login-form-input.errore > span::before {
content: "\f00d";
color: #4d40ff;
}
<form id="signinform" method="post" action="#" class="wp-user-form" autocomplete="off">
<div class="msc-login-form-input">
<input type="text" name="log" value="" id="user_login" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input">
<input type="password" name="pwd" value="" id="user_pass" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input-sendh">
<input type="submit" id="submit-login" name="submit-login" value="Submit" class="user-submit" />
</div>
</form>
此外,您的脚本标签不应位于表单内。它们 should 位于页面底部或使用异步 <head>
。
在我的简单表单中,我使用了这个简单的客户端验证。 当我按下 SUBMIT(更改样式输入和表单跨度)时,验证开始。
即使用户没有通过 SUBMIT 就填写了字段,我如何验证输入?
风格
<style>
.msc-login-form-input {
display: flex;
}
.msc-login-form-input.success > input {
color: #3F4254;
background-color: #ffffff;
}
.msc-login-form-input.errore > input {
background-color: #4d40ff;
color: #ffffff;
}
.msc-login-form-input.errore > input::-webkit-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input:-ms-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input::placeholder {
color: #ffffff;
}
.msc-login-form-input > span {
width: 35px;
background-color: rgba(0,0,0,0.05);
min-height: 100%;
align-items: center;
justify-content: center;
text-align: center;
display: flex;
}
.msc-login-form-input > span::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f100";
}
.msc-login-form-input.success > span::before {
content: "\f00c";
color:#FF1493;
}
.msc-login-form-input.errore > span::before {
content: "\f00d";
color:#4d40ff;
}
</style>
HTML 和 JS 此脚本检查表单的内部元素。如果,当我点击 SUBMIT 时字段为空,那么它会突出显示具有不同样式的输入并在 SPANs 标签中加载不同的图标。
<form id="signinform" method="post" action="#" class="wp-user-form" autocomplete="off">
<div class="msc-login-form-input">
<input type="text" name="log" value="" id="user_login" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input">
<input type="password" name="pwd" value="" id="user_pass" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input-sendh">
<input type="submit" id="submit-login" name="submit-login" value="Submit" class="user-submit" />
</div>
<script>
// ___________________________________________________________________
// validate contact form
const myform = document.getElementById('signinform');
myform.noValidate = true;
// custom form validation
myform.addEventListener('submit', validateForm);
// stop submission of valid form for demo
myform.addEventListener('submit', e => {
e.preventDefault();
const fd = new FormData(e.target);
for (const [name, value] of fd.entries()) {
console.log(name + ': ' + value);
}
});
// form validation
function validateForm(e) {
const
form = e.target,
field = Array.from(form.elements);
// reset fields
field.forEach(i => {
i.parentElement.classList.remove('errore');
i.parentElement.classList.add('success');
});
if (!form.checkValidity()) {
// form is invalid - cancel submit
e.preventDefault();
e.stopImmediatePropagation();
// apply invalid class
field.forEach(i => {
if (!i.checkValidity()) {
// field is invalid - add class
i.parentElement.classList.add('errore');
i.parentElement.classList.remove('success');
}
});
}
}
</script>
</form>
谢谢
根据您的评论。您可以添加侦听 Keydown(或 Keyup)的事件侦听器,然后删除显示错误的 class,而不是再次进行 运行 验证。
const myform = document.getElementById("signinform");
myform.noValidate = true;
// custom form validation
myform.addEventListener("submit", validateForm);
// stop submission of valid form for demo
myform.addEventListener("submit", (e) => {
e.preventDefault();
const fd = new FormData(e.target);
for (const [name, value] of fd.entries()) {
console.log(name + ": " + value);
}
});
// form validation
function validateForm(e) {
const form = e.target,
field = Array.from(form.elements);
// reset fields
field.forEach((i) => {
i.parentElement.classList.remove("errore");
i.parentElement.classList.add("success");
});
if (!form.checkValidity()) {
// form is invalid - cancel submit
e.preventDefault();
e.stopImmediatePropagation();
// apply invalid class
field.forEach((i) => {
if (!i.checkValidity()) {
// field is invalid - add class
i.parentElement.classList.add("errore");
i.parentElement.classList.remove("success");
}
});
}
}
// remove the error class on Keydown input
const formInputs = document.querySelectorAll(".msc-login-form-input");
formInputs.forEach((input) => {
input.addEventListener("keydown", () => {
input.classList.remove("errore");
input.classList.add("success");
});
});
.msc-login-form-input {
display: flex;
}
.msc-login-form-input.success > input {
color: #3f4254;
background-color: #ffffff;
}
.msc-login-form-input.errore > input {
background-color: #4d40ff;
color: #ffffff;
}
.msc-login-form-input.errore > input::-webkit-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input:-ms-input-placeholder {
color: #ffffff;
}
.msc-login-form-input.errore > input::placeholder {
color: #ffffff;
}
.msc-login-form-input > span {
width: 35px;
background-color: rgba(0, 0, 0, 0.05);
min-height: 100%;
align-items: center;
justify-content: center;
text-align: center;
display: flex;
}
.msc-login-form-input > span::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f100";
}
.msc-login-form-input.success > span::before {
content: "\f00c";
color: #ff1493;
}
.msc-login-form-input.errore > span::before {
content: "\f00d";
color: #4d40ff;
}
<form id="signinform" method="post" action="#" class="wp-user-form" autocomplete="off">
<div class="msc-login-form-input">
<input type="text" name="log" value="" id="user_login" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input">
<input type="password" name="pwd" value="" id="user_pass" placeholder="prova" required/>
<span></span> </div>
<div class="msc-login-form-input-sendh">
<input type="submit" id="submit-login" name="submit-login" value="Submit" class="user-submit" />
</div>
</form>
此外,您的脚本标签不应位于表单内。它们 should 位于页面底部或使用异步 <head>
。