Вопрос

Possible Duplicate:
HTML5 form required attribute. Set custom validation message?

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?

Это было полезно?

Решение

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("");
        };
    }
})
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top