<form id="form">
<div>
必須
<input type="text" required/>
</div>
<div>
email + 必須
<input type="email" required/>
</div>
<div>
全角カタカナ + 必須
<input type="text" pattern="[\u30A1-\u30FF]*" required/>
</div>
<button type="submit">送信</button>
</form>
input,
select {
&:valid {
border: 1px solid green;
}
&:invalid {
border: 1px solid red;
}
}
// 案件上デフォルトのstyleで使われることがほぼないのでstyleは適当
[type='radio'],
[type='checkbox'] {
&:valid {
box-shadow: green 0px 0px 3px 1px;
}
&:invalid {
box-shadow: red 0px 0px 3px 1px;
}
}
View Compiled
const $form = document.getElementById('form');
// 親要素(form)にイベントをつけると子要素のイベントが拾える
$form.addEventListener('change', update);
$form.addEventListener('input', update);
function update(e) {
let type = e.target.type;
let validity = e.target.validity;
console.log(type, validity);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.