How to allow only numeric (0-9) in HTML inputbox using jQuery?

前端 未结 30 1623
一生所求
一生所求 2020-11-21 05:38

I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9.

How can I do this using jQuery

相关标签:
30条回答
  • 2020-11-21 05:52

    try it within html code it self like onkeypress and onpast

    <input type="text" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onpaste="return false">
    
    0 讨论(0)
  • 2020-11-21 05:52

    Here is an answer that uses jQuery UI Widget factory. You can customize what characters are allowed easily.

    $('input').numberOnly({
        valid: "0123456789+-.$,"
    });
    

    That would allow numbers, number signs and dollar amounts.

    $.widget('themex.numberOnly', {
        options: {
            valid : "0123456789",
            allow : [46,8,9,27,13,35,39],
            ctrl : [65],
            alt : [],
            extra : []
        },
        _create: function() {
            var self = this;
    
            self.element.keypress(function(event){
                if(self._codeInArray(event,self.options.allow) || self._codeInArray(event,self.options.extra))
                {
                    return;
                }
                if(event.ctrlKey && self._codeInArray(event,self.options.ctrl))
                {
                    return;
                }
                if(event.altKey && self._codeInArray(event,self.options.alt))
                {
                    return;
                }
                if(!event.shiftKey && !event.altKey && !event.ctrlKey)
                {
                    if(self.options.valid.indexOf(String.fromCharCode(event.keyCode)) != -1)
                    {
                        return;
                    }
                }
                event.preventDefault(); 
            });
        },
    
        _codeInArray : function(event,codes) {
            for(code in codes)
            {
                if(event.keyCode == codes[code])
                {
                    return true;
                }
            }
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-11-21 05:53

    You can use on input event like this:

    $(document).on("input", ".numeric", function() {
        this.value = this.value.replace(/\D/g,'');
    });
    

    But, what's this code privilege?

    • It works on mobile browsers(keydown and keyCode have problem).
    • It works on AJAX generated content too, because We're using "on".
    • Better performance than keydown, for example on paste event.
    0 讨论(0)
  • 2020-11-21 05:56

    You could just use a simple JavaScript regular expression to test for purely numeric characters:

    /^[0-9]+$/.test(input);
    

    This returns true if the input is numeric or false if not.

    or for event keycode, simple use below :

         // Allow: backspace, delete, tab, escape, enter, ctrl+A and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
    
        var charValue = String.fromCharCode(e.keyCode)
            , valid = /^[0-9]+$/.test(charValue);
    
        if (!valid) {
            e.preventDefault();
        }
    
    0 讨论(0)
  • 2020-11-21 05:57

    Simple way to check that enter value is numeric is:

    var checknumber = $('#textbox_id').val();
    
        if(jQuery.isNumeric(checknumber) == false){
            alert('Please enter numeric value');
            $('#special_price').focus();
            return;
        }
    
    0 讨论(0)
  • 2020-11-21 05:57

    Just need to apply this method in Jquery and you can validate your textbox to just accept number only.

    function IsNumberKeyWithoutDecimal(element) {    
    var value = $(element).val();
    var regExp = "^\\d+$";
    return value.match(regExp); 
    }
    

    Try this solution here

    0 讨论(0)
提交回复
热议问题