diff --git a/public/js/form-validation.js b/public/js/form-validation.js new file mode 100644 index 00000000..9df36e6e --- /dev/null +++ b/public/js/form-validation.js @@ -0,0 +1,19 @@ +/* global showLoader */ + +// Form validation for GitHub username search +document.getElementById('search-form').onsubmit = function() { + const inputField = document.getElementById('username'); + // Auto-remove all spaces from the username + const username = inputField.value.replace(/\s/g, ''); + + if (username === '') { + alert('Username cannot be empty.'); + return false; // Prevent form submission + } + + // Update the input field with the cleaned value + inputField.value = username; + + showLoader(); + return true; // Allow form submission +}; diff --git a/src/pages/partials/body.handlebars b/src/pages/partials/body.handlebars index 004186c3..daa35ad7 100644 --- a/src/pages/partials/body.handlebars +++ b/src/pages/partials/body.handlebars @@ -10,11 +10,11 @@