Demo: Validate a Text Input with JavaScript

This demo keeps the original purpose: submit the form with the text box empty to see client-side validation stop the submission. Enter text and submit again to pass the check.

Submit the form by clicking the button.

HTML Used in the Demo Top ↑

<form id="textValidationDemo">
  <input type="text" id="textinput" name="textinput">
  <div id="my_msg" aria-live="polite"></div>
  <button type="submit">Submit</button>
</form>

JavaScript Used in the Demo Top ↑

const form = document.getElementById("textValidationDemo");
const input = document.getElementById("textinput");
const message = document.getElementById("my_msg");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  if (input.value.trim() === "") {
    message.textContent = "Fill the text box before submitting.";
    input.focus();
    return;
  }
  message.textContent = "Validation passed.";
});

JavaScript validation tutorial




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer