|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Input</title> |
| 7 | + <link rel="stylesheet" href="../../common.css" /> |
| 8 | + <script src="../../common.js"></script> |
| 9 | + <script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@8/dist/ionic/ionic.esm.js"></script> |
| 10 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@8/css/ionic.bundle.css" /> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body> |
| 14 | + <div class="container"> |
| 15 | + <form id="my-form"> |
| 16 | + <ion-checkbox helper-text="This needs to be checked" error-text="This field is required"> |
| 17 | + I agree to the terms and conditions |
| 18 | + </ion-checkbox> |
| 19 | + |
| 20 | + <br /> |
| 21 | + |
| 22 | + <ion-button type="submit" size="small">Submit</ion-button> |
| 23 | + </form> |
| 24 | + </div> |
| 25 | + |
| 26 | + <script> |
| 27 | + const form = document.getElementById('my-form'); |
| 28 | + const checkbox = form.querySelector('ion-checkbox'); |
| 29 | + |
| 30 | + form.addEventListener('submit', (event) => submit(event)); |
| 31 | + checkbox.addEventListener('ionChange', (event) => validateCheckbox(event)); |
| 32 | + |
| 33 | + const validateCheckbox = (event) => { |
| 34 | + checkbox.classList.add('ion-touched'); |
| 35 | + |
| 36 | + if (!event.detail.checked) { |
| 37 | + checkbox.classList.add('ion-invalid'); |
| 38 | + checkbox.classList.remove('ion-valid'); |
| 39 | + } else { |
| 40 | + checkbox.classList.remove('ion-invalid'); |
| 41 | + checkbox.classList.add('ion-valid'); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const submit = (event) => { |
| 46 | + event.preventDefault(); |
| 47 | + |
| 48 | + validateCheckbox({ detail: { checked: checkbox.checked } }); |
| 49 | + }; |
| 50 | + </script> |
| 51 | + </body> |
| 52 | +</html> |
0 commit comments