Как правильно выполнить проверку chekbox через if()?
'use strict'
let login = document.getElementById('fnameInput');
let email = document.getElementById('lname');
let password_1 = document.getElementById('password_1');
let password_2 = document.getElementById('password_2');
login.focus();
function validate() {
if (login.value != '') {
if (email.value != '') {
if (password_1.value != '' && password_2.value != '') {
if (password_1.value == password_2.value) {
let infoPassword = document.getElementById('information');
infoPassword.classList.remove('badregistration')
infoPassword.classList.add('registration');
infoPassword.innerHTML = 'вы успешно зарегистрировались!';
return true;
}
}
let infoPassword = document.getElementById('information');
infoPassword.classList.add('badregistration');
infoPassword.innerHTML = 'пароли не совпадают';
return false;
}
let infoPassword = document.getElementById('information');
infoPassword.classList.add('badregistration');
infoPassword.innerHTML = 'ВЫ НЕ ВВЕЛИ @email!';
return false;
}
let infoPassword = document.getElementById('information');
infoPassword.classList.add('badregistration');
infoPassword.innerHTML = 'ВЫ НЕ ВВЕЛИ ЛОГИН!';
return false;
}
.method_registration {
text-align: center;
margin: 0 auto;
height: 40%;
width: 30%;
min-height: 400px;
min-width: 300px;
max-width: 600px;
max-width: 400px;
background: rgb(225, 222, 243);
display: flex;
flex-direction: column;
border-radius: 10px;
}
.h1Form {
font-family: 'Open Sans', sans-serif;
color: rgb(58, 52, 62);
}
.form {
color: white;
border: none;
outline: none;
text-align: center;
margin: 0 auto;
width: 80%;
height: 50px;
background: rgb(58, 52, 62);
}
span {
font-family: 'Open Sans', sans-serif;
}
.checkbox {
background: green;
}
.registration {
font-family: 'Open Sans', sans-serif;
color: green;
}
.badregistration {
font-family: 'Open Sans', sans-serif;
color: red;
}
.inputForm {
font-family: 'Open Sans', sans-serif;
color: white;
border-radius: 5px;
border: none;
outline: none;
background: rgb(69, 65, 220);
margin: 20px auto;
padding: 20px;
width: 200px;
}
<form method="post" class="method_registration">
<h1 class="h1Form">Регистрация</h1>
<p class="formP"><input placeholder="логин" class="form" type="fname" name="fname" required id="fnameInput"></p>
<p class="formP"><input placeholder="email" class="form" type="lname" name="lname" required id="lname"></p>
<p class="formP"><input placeholder="пароль" class="form" type="password" required name="password_1" id="password_1"></p>
<p class="formP"><input placeholder="пароль" class="form" type="password" required name="password_2" id="password_2"></p>
<input class="inputForm" value="Регистрация" type="button" onclick="validate()">
<p><input id="checkbox" type="checkbox" name="checkbox"><span>Я не робот</span>
<p id="information"></p>
</form>
Ответы (1 шт):
Автор решения: Daniil Loban
→ Ссылка
За это отвечает значение свойства checked
const btn = document.querySelector('button');
const chk = document.querySelector('input');
const p = document.querySelector('p');
btn.addEventListener('click', ()=> {
if (chk.checked) {
p.textContent ="да"
} else {
p.textContent ="нет"
}
}, false)
<input type="checkbox">
<button type="button">проверить</button>
<p></p>