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