Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Custom Inline Error Message for Empty Search Field #314

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@
border-bottom: 1px solid black;;
}

.error-message {
color: #FF0000; /* Red color for error */
font-size: 0.9em;
margin-top: 5px;
display: none;
}

.error-input, .error-input:focus {
border-color: #FF0000 !important; /* Red border for error */
}

.error-message-display {
display: block;
}

/* Responsive Design */
@media (min-width: 601px) {
.body-search {
Expand Down
27 changes: 23 additions & 4 deletions src/assets/js/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
document.addEventListener("DOMContentLoaded", function (e) {
const searchForm = document.getElementById("searchForm");

const inputValidator = (event) => {
const queryInput = document.getElementById('query');
const errorMessage = document.getElementById('error-message');

// Validate input value
const hasInput = queryInput?.value.trim();

if (!hasInput) {
event.preventDefault(); // Prevent form submission
errorMessage.textContent = "Please enter a search term.";
errorMessage.classList.add('error-message-display');
queryInput?.classList.add('error-input');
} else {
errorMessage.classList.remove('error-message-display');
queryInput?.classList.remove('error-input');
}
};

// Event listeners
document.querySelector('.search-button')?.addEventListener('click', inputValidator);
document.querySelector('#query')?.addEventListener('input', inputValidator);


searchForm.addEventListener("submit", (e) => {
e.preventDefault();

Expand Down Expand Up @@ -31,10 +54,6 @@ document.addEventListener("DOMContentLoaded", function (e) {

// construct the URL
function buildURL(form) {
if (form.query === "") {
alert("Please enter a search value");
return;
}
if (form.searchEngineURL === undefined) {
alert("Please select a search engine");
return;
Expand Down
8 changes: 2 additions & 6 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,10 @@ <h2>Our Work Relies On You!</h2>
<div class="body-first-sect">
<div class="body-search">
<h3>Find content you can share, use, and remix.</h3>
<input
type="text"
id="query"
name="query"
value=""
placeholder="Enter your search query" />
<input type="text" id="query" name="query" value="" placeholder="Enter your search query" required />
<button class="search-button">Search</button>
</div>
<div id="error-message" class="error-message error-message-display"></div>
<div class="body-search-description" role="contentinfo">
<p>
The Search Portal helps you search CC licensed works across multiple platforms.
Expand Down