Is there a minlength validation attribute in HTML5?

前端 未结 17 1475
日久生厌
日久生厌 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:26

    minlength attribute is now widely supported in most of the browsers.

    <input type="text" minlength="2" required>
    

    But, as with other HTML5 features, IE11 is missing from this panorama. So, if you have a wide IE11 user base, consider using the pattern HTML5 attribute that is supported almost across the board in most browsers (including IE11).

    To have a nice and uniform implementation and maybe extensible or dynamic (based on the framework that generate your HTML), I would vote for the pattern attribute:

    <input type="text" pattern=".{2,}" required>
    

    There is still a small usability catch when using pattern. The user will see a non-intuitive (very generic) error/warning message when using pattern. See this jsfiddle or below:

    <h3>In each form type 1 character and press submit</h3>
    </h2>
    <form action="#">
      Input with minlength: <input type="text" minlength="2" required name="i1">
      <input type="submit" value="Submit">
    </form>
    <br>
    <form action="#">
      Input with patern: <input type="text" pattern=".{2,}" required name="i1">
      <input type="submit" value="Submit">
    </form>

    For example, in Chrome (but similar in most browsers), you will get the following error messages:

    Please lengthen this text to 2 characters or more (you are currently using 1 character)
    

    by using minlength and

    Please match the format requested
    

    by using pattern.

    0 讨论(0)
  • 2020-11-22 17:28

    My solution for textarea using jQuery and combining HTML5 required validation to check the minimum length.

    minlength.js

    $(document).ready(function(){
      $('form textarea[minlength]').on('keyup', function(){
        e_len = $(this).val().trim().length
        e_min_len = Number($(this).attr('minlength'))
        message = e_min_len <= e_len ? '' : e_min_len + ' characters minimum'
        this.setCustomValidity(message)
      })
    })
    

    HTML

    <form action="">
      <textarea name="test_min_length" id="" cols="30" rows="10" minlength="10"></textarea>
    </form>
    
    0 讨论(0)
  • 2020-11-22 17:33

    Not HTML5, but practical anyway: if you happen to use AngularJS, you can use ng-minlength for both inputs and textareas. See also this Plunk.

    0 讨论(0)
  • 2020-11-22 17:34

    The minLength attribute (unlike maxLength) does not exist natively in HTML5. However, there a some ways to validate a field if it contains less than x characters.

    An example is given using jQuery at this link: http://docs.jquery.com/Plugins/Validation/Methods/minlength

    <html>
        <head>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
            <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
            <script type="text/javascript">
                jQuery.validator.setDefaults({
                    debug: true,
                    success: "valid"
                });;
            </script>
    
            <script>
                $(document).ready(function(){
                    $("#myform").validate({
                        rules: {
                            field: {
                                required: true,
                                minlength: 3
                            }
                        }
                    });
                });
            </script>
        </head>
    
        <body>
            <form id="myform">
                <label for="field">Required, Minimum length 3: </label>
                <input class="left" id="field" name="field" />
                <br/>
                <input type="submit" value="Validate!" />
            </form>
        </body>
    
    </html>
    
    0 讨论(0)
  • 2020-11-22 17:36

    New version:

    It extends the use (textarea and input) and fixes bugs.

    // Author: Carlos Machado
    // Version: 0.2
    // Year: 2015
    window.onload = function() {
        function testFunction(evt) {
            var items = this.elements;
            for (var j = 0; j < items.length; j++) {
                if ((items[j].tagName == "INPUT" || items[j].tagName == "TEXTAREA") && items[j].hasAttribute("minlength")) {
                    if (items[j].value.length < items[j].getAttribute("minlength") && items[j].value != "") {
                        items[j].setCustomValidity("The minimum number of characters is " + items[j].getAttribute("minlength") + ".");
                        items[j].focus();
                        evt.defaultPrevented;
                        return;
                    }
                    else {
                        items[j].setCustomValidity('');
                    }
                }
            }
        }
        var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
        var isChrome = !!window.chrome && !isOpera;
        if(!isChrome) {
            var forms = document.getElementsByTagName("form");
            for(var i = 0; i < forms.length; i++) {
                forms[i].addEventListener('submit', testFunction,true);
                forms[i].addEventListener('change', testFunction,true);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题