Выделение рамкой обязательной отметки чекбокса
Как добавить чекбоксу красную рамку (при нажатии на регистрацию), если он не отмечен?
'use strict'
const email_wrap = document.getElementById('email_label')
const pass_wrap = document.getElementById('pass_label')
const checkbox_wrap = document.getElementById('label_checkbox')
const email = document.getElementById('email')
const pass = document.getElementById('password')
const chekbox = document.getElementById('checkbox')
function validateElement(val, condition, wrap) {
wrap.classList.remove('error', 'error-novalue')
wrap.classList.remove('error', 'error-notvalid')
if (!val) {
wrap.classList.add('error', 'error-novalue')
} else if (!condition) {
wrap.classList.add('error', 'error-notvalid')
}
}
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\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,}))$/;
return re.test(String(email).toLowerCase());
}
document.getElementById('btn').addEventListener('click', (e) => {
e.preventDefault()
validateElement(email.value, validateEmail(email.value), email_label)
validateElement(pass.value, pass.value.length >= 8, pass_label)
validateElement(chekbox.checked, true, label_checkbox)
console.log({
email: email.value,
password: pass.value
})
})
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100%;
height: 100vh;
background: #1B2473;
color: #787878;
font-family: Roboto;
font-size: 16px;
}
.wrapper {
width: 100%;
height: 100%;
padding-top: 82px;
}
.form {
width: 600px;
height: 550px;
margin: 0 auto;
background: #fff;
box-shadow: 0 0 40px 10px rgba(0, 0, 0, 0.25);
border-radius: 20px;
padding: 25px 45px;
}
.form__title {
text-align: center;
font-size: 50px;
font-weight: bold;
font-style: normal;
line-height: 100%;
margin-bottom: 17px;
}
.form__group {}
/*----------Проверить-----------*/
.form__label-input {
display: block;
margin-bottom: 29px;
}
/*--------------------------------- */
.form__label-input span {
display: block;
font-style: normal;
font-weight: bold;
font-size: 16px;
line-height: 1;
margin-bottom: 3px;
padding-left: 8px;
}
.form__input {
display: block;
background: #FFFFFF;
border: 2px solid #787878;
border-radius: 10px;
height: 49px;
width: 100%;
padding: 7px 0 6px 15px;
outline: none;
font-style: normal;
font-weight: bold;
font-size: 28px;
line-height: 36px;
}
.form__input::placeholder {
font-weight: bold;
line-height: 129%;
color: #cccccc;
font-size: 28px;
font-style: normal;
}
.form__label-checkbox {
width: 100%;
display: flex;
align-items: center;
margin-bottom: 50px;
flex-wrap: wrap;
}
.form__checkbox {
visibility: hidden;
width: 24px;
height: 24px;
position: relative;
margin-right: 10px;
flex-shrink: 0;
}
.form__label-checkbox span {
font-style: normal;
font-weight: normal;
font-size: 14px;
line-height: 16px;
}
.form__label-checkbox a {
color: inherit;
}
.form__checkbox::after {
content: '';
background: #FFFFFF;
border: 2px solid #787878;
box-sizing: border-box;
border-radius: 7px;
position: absolute;
top: 0;
left: 0;
width: 24px;
height: 24px;
padding-left: 4px;
visibility: visible;
}
.form__checkbox:checked::after {
content: '✓';
}
.form__btn {
border: none;
background: #1A226B;
border-radius: 10px;
font-style: normal;
font-weight: normal;
font-size: 24px;
line-height: 16px;
color: #fff;
height: 58px;
width: 180px;
text-align: center;
margin: 0 auto;
display: block;
cursor: pointer;
}
/*----------Media-----------*/
@media (max-width: 768px) {
.wrapper {
padding: 20px;
}
.form {
width: 100%;
height: 100%;
padding-top: 38px;
}
.form__title {
font-size: 60px;
line-height: 1;
margin-bottom: 13px;
}
.form__label-input span {
font-size: 24px;
}
.form__label-input:last-of-type {
margin-bottom: 19px;
}
.form__label-checkbox {
margin-bottom: 64px;
}
.form__checkbox input {
width: 32px;
height: 32px;
}
.form__label-checkbox span {
font-size: 18px;
}
.form__btn {
width: 220px;
height: 80px;
font-size: 32px;
}
}
.form__label-input,
.form__label-checkbox {
position: relative;
}
.form__label-input::after,
.form__label-checkbox::after {
content: '*';
display: block;
position: absolute;
top: 25px;
left: -15px;
}
.form__label-checkbox::after {
content: '*';
display: block;
position: absolute;
top: 0;
left: -15px;
}
/*----------ERRORS------------*/
.error.form__label-input::after,
.error.form__label-checkbox::after {
color: #CB2424;
}
span.errors,
span.errors_valid {
display: none;
color: #CB2424;
font-size: 10px;
font-weight: 500;
line-height: 16px;
width: 100%;
}
.error.form__label-input span {
color: #CB2424;
}
.error.form__label-input input,
.error.form__label-checkbox checkbox {
border-color: #CB2424;
}
.error-novalue span.errors {
display: block;
}
.error-notvalid span.errors_valid {
display: block;
}
.error-novalue,
.error-notvalid {
margin-bottom: 13px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto:regular,700&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="./style.css">
<title>Form</title>
</head>
<body>
<main class="wrapper">
<form action="#" class="form">
<h1 class="form__title">Регистрация</h1>
<div class="form__group">
<label for="" class="form__label-input" id="email_label">
<span>Email</span>
<input class="form__input" id="email" name="email" type="text" placeholder="Введите Email">
<span class="errors">Поле обязательно для заполнения</span>
<span class="errors_valid">Email невалидный</span>
</label>
</div>
<div class="form__group">
<label for="" class="form__label-input" id="pass_label">
<span>Пароль</span>
<input class="form__input" id="password" name="pass" type="password" placeholder="Введите пароль">
<span class="errors">Поле обязательно для заполнения</span>
<span class="errors_valid">Пароль должен содержать как минимум 8 символов</span>
</label>
</div>
<div class="form__group">
<label class="form__label-checkbox" id="label_checkbox">
<input class="form__checkbox" id="checkbox" name="checkbox" type="checkbox">
<span>Я согласен c <a href="#">Правилами пользования приложением</a> </span>
<span class="errors">Поле обязательно для заполнения</span>
</label>
</div>
<div class="form__button-block">
<button class="form__btn" id="btn" type="submit">Регистрация</button>
</div>
</form>
</main>
<script src="./script1.js"></script>
</body>
</html>
Ответы (1 шт):
Автор решения: Alexandr_Yakovlev
→ Ссылка
Попробуйте добавить этот кусок кода в конец вашего css файла
.error-novalue .form__checkbox::after,
.error-notvalid .form__checkbox::after {
border-color: #CB2424;
}