-1

I have a form with an input as follows:

<input id="username" name="username" required>

Is it possible to set a custom validation message for the required field?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Michael
  • 32,527
  • 49
  • 210
  • 370

1 Answers1

0

This question has already been answered Here


Use setCustomValidity:

$(document).ready(function() {
    var elements = document.getElementsByTagName("INPUT");
    for (var i = 0; i < elements.length; i++) {
        elements[i].oninvalid = function(e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("This field cannot be left blank");
            }
        };
        elements[i].oninput = function(e) {
            e.target.setCustomValidity("");
        };
    }
})
Community
  • 1
  • 1
cbronson
  • 366
  • 1
  • 9