check for whitespaces in login and registration

This commit is contained in:
Florian Hoss 2022-04-08 06:48:44 +02:00
parent 4a07b461d3
commit 3e59c0601e

View file

@ -71,7 +71,7 @@
<input type="text" class="form-control" id="username" <input type="text" class="form-control" id="username"
aria-describedby="inputGroupPrepend" required> aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback"> <div class="invalid-feedback">
Please choose a username. Please choose a username. It cannot include whitespaces.
</div> </div>
</div> </div>
</div> </div>
@ -79,7 +79,7 @@
<label for="password" class="form-label">Password</label> <label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" aria-describedby="passwordHelp" required> <input type="password" class="form-control" id="password" aria-describedby="passwordHelp" required>
<div id="invalidPassword" class="invalid-feedback"> <div id="invalidPassword" class="invalid-feedback">
Please choose a password. Please choose a password. It cannot include whitespaces.
</div> </div>
</div> </div>
<div class="col-12"> <div class="col-12">
@ -169,21 +169,32 @@
{{define "formScripts"}} {{define "formScripts"}}
<script> <script>
let form = document.querySelector('.needs-validation') let form = document.querySelector('.needs-validation')
function hasWhiteSpace(s) {
return s.indexOf(' ') >= 0;
}
if (form) { if (form) {
form.addEventListener('submit', function (event) { form.addEventListener('submit', function (event) {
if (!form.checkValidity()) { if (!form.checkValidity()) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
} else { } else {
let invalid = false;
event.preventDefault(); event.preventDefault();
let data = new FormData(); let data = new FormData();
let form_element = document.getElementsByClassName('form-control'); let form_element = document.getElementsByClassName('form-control');
for (let i = 0; i < form_element.length; i++) { for (let i = 0; i < form_element.length; i++) {
data.append(form_element[i].id, form_element[i].value); data.append(form_element[i].id, form_element[i].value);
} }
submitForm(data); for (let i = 0; i < form_element.length; i++) {
if (hasWhiteSpace(form_element[i].value)) {
form_element[i].classList.add('is-invalid')
invalid = true;
}
}
!invalid && submitForm(data);
} }
form.classList.add('was-validated');
}, false); }, false);
} }
</script> </script>