0

In Firefox when a text control has the attribute "required" a popup appears when the user does not enter any text:

            <input id="foo" type="text" name="foo" required>

validation example

Is it possible with Javascript to invoke the same such popup but with a custom message? Something like:

document.getElementById("foo").?message = "Custom message goes here!";
document.getElementById("foo").?invoke();
gornvix
  • 3,154
  • 6
  • 35
  • 74

1 Answers1

1

There are many possible ways to do this and almost coutless validation libraries. The absolute simplest and still relying on html5 is utilizing oninvalid and setCustomValidity.

Support is pretty much 100% at this point - http://caniuse.com/#feat=form-validation

<form>
  <input id="foo" type="text" name="foo" required oninvalid="this.setCustomValidity('Custom message goes here!')"/>
  <input type="submit"/>
</form>
dmoo
  • 1,509
  • 13
  • 16