HTML5 form required attribute. Set custom validation message?

前端 未结 14 1654
你的背包
你的背包 2020-11-22 01:26

I\'ve got the following HTML5 form: http://jsfiddle.net/nfgfP/

14条回答
  •  情深已故
    2020-11-22 01:56

    I have a simpler vanilla js only solution:

    For checkboxes:

    document.getElementById("id").oninvalid = function () {
        this.setCustomValidity(this.checked ? '' : 'My message');
    };
    

    For inputs:

    document.getElementById("id").oninvalid = function () {
        this.setCustomValidity(this.value ? '' : 'My message');
    };
    

提交回复
热议问题