1

I have an input:

<input type="text" pattern="[a-zA-Z ]{0,20}" oninvalid="setCustomValidity('Please insert only letters')" required>

If I input a number, the title will appear. But if I don't input anything, it will give the same error title. How can the title when I input not only letters is different when I doesn't input anyhing?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Bariq Dharmawan
  • 755
  • 4
  • 16
  • 26

1 Answers1

1

I think that your problem is similar to the one in this link.

The correct answer there says that:

If you set a value with setCustomValidity() then the field is invalid. That is setting a non-zero length string causes the browser to consider the field invalid. In order to allow for the effects of any other validations you have to clear the custom validity:

  <input type="password" name="user_password_new" pattern=".{6,}" required oninvalid="setCustomValidity('Minimum length is 6 characters')" oninput="setCustomValidity('')" />

Here is a working example for your case to see:

<form>
  <input type="text" pattern="[a-zA-Z ]{0,20}" oninvalid="setCustomValidity('Please insert only letters')" required oninput="setCustomValidity('')">
  <button type="submit">Submit</button>
</form>
Community
  • 1
  • 1
Ammar Alyousfi
  • 4,112
  • 5
  • 31
  • 42