Is there a minlength validation attribute in HTML5?

前端 未结 17 1754
日久生厌
日久生厌 2020-11-22 17:10

It seems the minlength attribute for an field doesn\'t work.

Is there any other attribute in HTML5 with the help of which I

17条回答
  •  一个人的身影
    2020-11-22 17:20

    I wrote this JavaScript code, [minlength.js]:

    window.onload = function() {
        function testaFunction(evt) {
            var elementos = this.elements;
            for (var j = 0; j < elementos.length; j++) {
                if (elementos[j].tagName == "TEXTAREA" && elementos[j].hasAttribute("minlength")) {
                    if (elementos[j].value.length < elementos[j].getAttribute("minlength")) {
                        alert("The textarea control must be at least " + elementos[j].getAttribute("minlength") + " characters.");
                        evt.preventDefault();
                    };
                }
            }
        }
        var forms = document.getElementsByTagName("form");
        for(var i = 0; i < forms.length; i++) {
            forms[i].addEventListener('submit', testaFunction, true);
        }
    }
    

提交回复
热议问题