Add input validation

This commit is contained in:
Florine W. Dekker 2021-12-01 22:00:21 +01:00
parent 85c6534a7e
commit 27419d2359
Signed by: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
4 changed files with 47 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "debreviator", "name": "debreviator",
"version": "0.0.2", "version": "0.0.3",
"description": "Creates meaning by undoing your abbreviation.", "description": "Creates meaning by undoing your abbreviation.",
"author": "F.W. Dekker", "author": "F.W. Dekker",
"browser": "dist/bundle.js", "browser": "dist/bundle.js",

View File

@ -1,3 +1,11 @@
:root {
--error-color: red;
}
#debreviations { #debreviations {
height: 20em; height: 20em;
} }
#abbreviation-error {
color: var(--error-color);
}

View File

@ -39,8 +39,14 @@
<input id="abbreviation" type="text" autofocus /> <input id="abbreviation" type="text" autofocus />
<button id="debreviate" type="button">Debreviate</button> <button id="debreviate" type="button">Debreviate</button>
</div>
</div>
<br />
<div class="row">
<div class="column">
<label for="debreviations">Debreviations</label> <label for="debreviations">Debreviations</label>
<span id="abbreviation-error"></span>
<textarea id="debreviations" readonly></textarea> <textarea id="debreviations" readonly></textarea>
</div> </div>
</div> </div>

View File

@ -22,9 +22,28 @@ doAfterLoad(() => {
}); });
doAfterLoad(() => { doAfterLoad(() => {
const abbreviationInput = $("#abbreviation");
const abbreviationError = $("#abbreviation-error");
const debreviateButton = $("#debreviate");
const debreviationOutput = $("#debreviations");
const showError = (message) => {
if (message === undefined) {
abbreviationError.innerText = "";
debreviationOutput.classList.remove("hidden");
} else {
abbreviationError.innerText = message;
debreviationOutput.classList.add("hidden");
}
};
const debreviate = () => { const debreviate = () => {
const abbreviation = $("#abbreviation").value.trim(); const abbreviation = abbreviationInput.value.trim();
if (abbreviation === "") return; if (abbreviation === "") {
showError("Abbreviation should be non-empty.");
return;
}
const regex = new RegExp(abbreviation.split("").join(".*")); const regex = new RegExp(abbreviation.split("").join(".*"));
@ -45,11 +64,19 @@ doAfterLoad(() => {
)[0] )[0]
) )
) )
.then(matches => $("#debreviations").value = matches.join("\n")); .then(matches => {
if (matches.length === 0) {
showError("No debreviations found.");
return;
}
debreviationOutput.value = matches.join("\n");
showError();
});
}; };
$("#debreviate").addEventListener("click", debreviate); debreviateButton.addEventListener("click", debreviate);
$("#abbreviation").addEventListener("keypress", event => { abbreviationInput.addEventListener("keypress", event => {
if (event.key === "Enter") { if (event.key === "Enter") {
debreviate(); debreviate();
event.preventDefault(); event.preventDefault();