How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2912
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

26条回答
  •  我寻月下人不归
    2020-11-22 05:30

    I know there's an answer already, but if you want your input to behave exactly like the maxlength attribute or as close as you can, use the following code:

    (function($) {
     methods = {
        /*
         * addMax will take the applied element and add a javascript behavior
         * that will set the max length
         */
        addMax: function() {
            // set variables
            var
                maxlAttr = $(this).attr("maxlength"),
                maxAttR = $(this).attr("max"),
                x = 0,
                max = "";
    
            // If the element has maxlength apply the code.
            if (typeof maxlAttr !== typeof undefined && maxlAttr !== false) {
    
                // create a max equivelant
                if (typeof maxlAttr !== typeof undefined && maxlAttr !== false){
                    while (x < maxlAttr) {
                        max += "9";
                        x++;
                    }
                  maxAttR = max;
                }
    
                // Permissible Keys that can be used while the input has reached maxlength
                var keys = [
                    8, // backspace
                    9, // tab
                    13, // enter
                    46, // delete
                    37, 39, 38, 40 // arrow keys<^>v
                ]
    
                // Apply changes to element
                $(this)
                    .attr("max", maxAttR) //add existing max or new max
                    .keydown(function(event) {
                        // restrict key press on length reached unless key being used is in keys array or there is highlighted text
                        if ($(this).val().length == maxlAttr && $.inArray(event.which, keys) == -1 && methods.isTextSelected() == false) return false;
                    });;
            }
        },
        /*
         * isTextSelected returns true if there is a selection on the page. 
         * This is so that if the user selects text and then presses a number
         * it will behave as normal by replacing the selection with the value
         * of the key pressed.
         */
        isTextSelected: function() {
           // set text variable
            text = "";
            if (window.getSelection) {
                text = window.getSelection().toString();
            } else if (document.selection && document.selection.type != "Control") {
                text = document.selection.createRange().text;
            }
            return (text.length > 0);
        }
    };
    
    $.maxlengthNumber = function(){
         // Get all number inputs that have maxlength
         methods.addMax.call($("input[type=number]"));
     }
    
    })($)
    
    // Apply it:
    $.maxlengthNumber();
    

提交回复
热议问题