This commit is contained in:
2024-05-24 13:46:06 +02:00
parent f394538776
commit cceab89523
14 changed files with 134 additions and 15 deletions
+46
View File
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once "../../base/meta.php" ?>
<title>Colour Selector</title>
<script src="./js/formValidation.js"></script>
<?php
// PHP
include_once "../../base/settings.php";
include_once "../../base/headers.php";
if (isset($_POST["colour"])) {
$colour = $_POST["colour"];
if (preg_match("^#([0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$", $colour) === false) {
$error = "Invalid hex colour";
}
}
Headers::html();
?>
</head>
<body style="background-color: <?=$colour?>">
<div class="form-check form-switch m-3">
<input class="form-check-input" type="checkbox" role="switch" id="clientSideValidate">
<label class="form-check-label" for="clientSideValidate">Use Client Side Validation</label>
</div>
<form method="post" class="needs-validation row m-0" novalidate>
<div class="mb-3">
<label class="form-label" for="colour">Colour Code</label>
<input class="form-control" id="colour" name="colour" required pattern="^#([0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$" />
<div class="invalid-feedback">
Please enter valid hex colour code
</div>
</div>
<button class="btn btn-primary mt-3" name="firstLoad" value="0">Berechnen</button>
</form>
</body>
</html>
@@ -0,0 +1,33 @@
"use strict";
document.addEventListener("DOMContentLoaded", () => {
const _switch = document.getElementById("clientSideValidate");
_switch.addEventListener("change", () =>
_switch.checked ? addValidation() : removeValidation()
);
});
const handler = (event) => {
if (!event.target.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
event.target.classList.add("was-validated");
};
const addValidation = () => {
const forms = document.querySelectorAll(".needs-validation");
Array.from(forms).forEach((form) => {
form.addEventListener("submit", handler, false);
});
};
const removeValidation = () => {
const forms = document.querySelectorAll(".needs-validation");
Array.from(forms).forEach((form) => {
form.removeEventListener("submit", handler, false);
});
};