health-check domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260JavaScript Foundations • StreetGeek Academy
Learn how to validate and handle form input with JavaScript, provide real-time feedback, and enhance your portfolio’s contact form UX.
Form validation ensures users enter correct information before submission.
It improves data quality, prevents errors, and enhances user experience.
user@example.com → validuserexample.com → invalidYou’ll learn both built-in HTML validation and custom JS validation.
Add this to your portfolio’s Contact page:
<form id="contactForm">
<label for="name">Name</label>
<input type="text" id="name" placeholder="Your name" required>
<label for="email">Email</label>
<input type="email" id="email" placeholder="you@example.com" required>
<label for="message">Message</label>
<textarea id="message" rows="4" placeholder="Write your message..." required></textarea>
<button type="submit">Send Message</button>
<p id="form-status"></p>
</form>
required attributes for basic HTML5 validation.
Use JavaScript to stop page reload and process the form manually.
const form = document.getElementById("contactForm");
const status = document.getElementById("form-status");
form.addEventListener("submit", (e) => {
e.preventDefault(); // Prevent reload
status.textContent = "Submitting...";
});
form.addEventListener("submit", (e) => {
e.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const message = document.getElementById("message").value.trim();
if (name === "" || email === "" || message === "") {
status.textContent = "⚠️ Please fill in all fields.";
status.style.color = "red";
return;
}
if (!email.includes("@") || !email.includes(".")) {
status.textContent = "⚠️ Please enter a valid email address.";
status.style.color = "red";
return;
}
status.textContent = "✅ Message sent successfully!";
status.style.color = "green";
form.reset();
});
Example: Live Email Check
const emailField = document.getElementById("email");
emailField.addEventListener("input", () => {
if (emailField.value.includes("@") && emailField.value.includes(".")) {
emailField.style.borderColor = "green";
} else {
emailField.style.borderColor = "red";
}
});
Regex gives you fine control over patterns like emails, phone numbers, and passwords.
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
status.textContent = "❌ Invalid email format.";
}
const phonePattern = /^\d{3}-\d{3}-\d{4}$/; // e.g., 555-123-4567
Instead of one status line, show errors near the inputs.
<div class="field">
<label for="name">Name</label>
<input id="name" type="text">
<small class="error"></small>
</div>
function showError(input, message) {
const small = input.nextElementSibling;
small.textContent = message;
small.style.color = "red";
}
form.addEventListener("submit", (e) => {
e.preventDefault();
const name = document.getElementById("name");
const email = document.getElementById("email");
if (name.value.trim() === "") {
showError(name, "Name is required");
} else {
showError(name, "");
}
if (!email.value.includes("@")) {
showError(email, "Enter a valid email");
} else {
showError(email, "");
}
});
Add a hidden field to trap bots:
<input type="text" id="website" style="display:none;">
const honeypot = document.getElementById("website");
if (honeypot.value !== "") {
return; // Stop spam bots
}
Use a small modal popup instead of text feedback:
if (formIsValid) {
alert("Thanks for reaching out! We'll reply soon.");
}
alert() with a styled modal or animation.
1) What does e.preventDefault() do?
2) How do you access an input’s value in JavaScript?
3) What is the purpose of a Regular Expression (Regex)?
4) How can you provide real-time feedback while the user types?
5) What should happen after successful form submission?
Make your contact form fully interactive:
const message = document.getElementById("message");
const statusSmall = document.createElement("small");
message.parentElement.appendChild(statusSmall);
message.addEventListener("input", () => {
const remaining = 200 - message.value.length;
statusSmall.textContent = `${remaining} characters remaining`;
});
Next up → MODULE 9: Fetch API, Async/Await & Local Storage
We’ll connect your portfolio to real data — like fetching live GitHub projects or storing user preferences (dark mode, likes, etc.) directly in the browser.