Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
How to prevent form validation error message but still validate?

I have looked all over the internet to solve this but can't find any answers. If I am not clear, here is what I want to remove:

Please fill out this field image

Example code:

<form action="search.html" id="form">
  <input type="text" placeholder="Search..." name="s" id="s" required>
  <button type="submit">Submit</button>
</form>

Also, for the <input type="">, is it better to put type="search" rather than type="text" for what I'm doing?
If anything other than HTML needs to be used, no jquery if possible please.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use setCustomValidity:

<form action="search.html" id="form">
  <input type="text" placeholder="Search..." name="s" id="s" oninvalid="this.setCustomValidity(' ')" required>
  <button type="submit">Submit</button>
</form>

I'm not sure why you have to specify a space (" ") as the validity message, but it apparently gets ignored if you apply an empty string.

about 4 years ago · Juan Pablo Isaza Report

0

If you want it to act like Google, then you can listen for the submit event on the form, and then use .preventDefault() to prevent the form from submitting if the input value is empty. See https://stackoverflow.com/a/8664535/1499877 for another example.

form = document.querySelector('form');
input = document.querySelector('input');

form.addEventListener('submit', function(event) {
  if (input.value == '') {
    event.preventDefault(); // prevent the form from submitting
  }      
});
<form>
  <input type="search" />
  <button>
    Submit?
  </button>
</form>

Another option with slightly better user experience is to disable the button by default, and then enable it when the text input field has some value. This at least provides the user with a little feedback (the button becomes enabled) when they enter something in the input field.

input = document.querySelector('input');
button = document.querySelector('button');

input.addEventListener('input', function(event) {
  if (input.value == '') {
    button.disabled = true;
  } else {
    button.disabled = false;
  }
});
<form>
  <input type="search" />
  <button disabled>
    Submit?
  </button>
</form>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!