|
1 | 1 | <script>
|
2 | 2 | import { goto } from '$app/navigation';
|
3 | 3 |
|
| 4 | + export let targetPath = '/einzelverssynopse'; |
| 5 | +
|
4 | 6 | /**
|
5 | 7 | * @type {HTMLInputElement}
|
6 | 8 | */
|
|
12 | 14 |
|
13 | 15 | let additional = '';
|
14 | 16 |
|
15 |
| - let valid = false; |
| 17 | + /** |
| 18 | + * @param {Event & { target: HTMLInputElement }} e |
| 19 | + */ |
| 20 | + function handleInput(e) { |
| 21 | + validateMinMax(e.target); |
| 22 | + } |
16 | 23 |
|
17 |
| - const validateMinMax = (e) => { |
18 |
| - console.log(e); |
19 |
| - console.log(e.target.value); |
20 |
| - console.log(isNaN(e.target.value)); |
| 24 | + const validateMinMax = (/** @type {HTMLInputElement } */ i) => { |
21 | 25 | //validate if input is a Number
|
22 |
| - if (isNaN(e.target.value) || e.target.value === '') { |
23 |
| - e.target.classList.add('input-error'); |
| 26 | + if (isNaN(Number(i.value)) || i.value === '') { |
| 27 | + i.classList.add('input-error'); |
| 28 | + return false; |
24 | 29 | } else {
|
25 | 30 | //validate if input is within min and max
|
26 |
| - if ( |
27 |
| - parseInt(e.target.value) < parseInt(e.target.min) || |
28 |
| - parseInt(e.target.value) > parseInt(e.target.max) |
29 |
| - ) { |
30 |
| - console.log(e.target.value); |
31 |
| - console.log(e.target.min); |
32 |
| - console.log(e.target.max); |
33 |
| -
|
34 |
| - e.target.classList.add('input-error'); |
| 31 | + if (parseInt(i.value) < parseInt(i.min) || parseInt(i.value) > parseInt(i.max)) { |
| 32 | + i.classList.add('input-error'); |
| 33 | + return false; |
35 | 34 | } else {
|
36 |
| - e.target.classList.remove('input-error'); |
| 35 | + i.classList.remove('input-error'); |
| 36 | + return true; |
37 | 37 | }
|
38 | 38 | }
|
39 | 39 | };
|
40 | 40 | </script>
|
41 | 41 |
|
42 | 42 | <form
|
43 | 43 | class="flex max-w-full items-baseline gap-1 my-3"
|
44 |
| - on:submit={(e) => { |
45 |
| - goto(`/einzelverssynopse/${thirties.value}/${verse.value}-${additional}`); |
| 44 | + on:submit|preventDefault={(e) => { |
| 45 | + if (validateMinMax(thirties) && validateMinMax(verse)) { |
| 46 | + goto(`${targetPath}/${thirties.value}/${verse.value}${additional ? '-' + additional : ''}`); |
| 47 | + } |
46 | 48 | }}
|
47 | 49 | >
|
48 | 50 | <p>Vers:</p>
|
|
52 | 54 | class="input inline max-w-28"
|
53 | 55 | min="1"
|
54 | 56 | max="827"
|
55 |
| - on:input={(e) => validateMinMax(e)} |
| 57 | + on:input={handleInput} |
56 | 58 | bind:this={thirties}
|
57 | 59 | />.<input
|
58 | 60 | type="number"
|
59 | 61 | placeholder="Vers"
|
60 | 62 | class="input max-w-20"
|
61 | 63 | min="1"
|
62 | 64 | max="30"
|
63 |
| - on:input={(e) => validateMinMax(e)} |
| 65 | + on:input={handleInput} |
64 | 66 | bind:this={verse}
|
65 | 67 | />-<input type="text" placeholder="Zusatz" class="input max-w-20" bind:value={additional} />
|
66 | 68 | <button class="btn-icon variant-filled btn-icon-sm flex-shrink-0 flex-grow-0">
|
|
0 commit comments