Best way to restrict a text field to numbers only?

前端 未结 29 1635
长情又很酷
长情又很酷 2020-12-04 22:01

I\'m using the following Javascript to restrict a text field on my website to only accept numerical input, and no other letters or characters. The problem is, it REALLY reje

相关标签:
29条回答
  • 2020-12-04 22:17

    This is my plugin for that case:

     (function( $ ) {
        $.fn.numbers = function(options) {
          $(this).keypress(function(evt){
              var setting = $.extend( {
                    'digits' : 8
                  }, options);
              if($(this).val().length > (setting.digits - 1) && evt.which != 8){
                  evt.preventDefault(); 
              }
              else{
                  if(evt.which < 48 || evt.which > 57){
                    if(evt.keyCode != 8){
                        evt.preventDefault();  
                    }
                  }
              }
          });
        };
      })( jQuery );
    

    Use:

     $('#limin').numbers({digits:3});
     $('#limax').numbers();
    
    0 讨论(0)
  • 2020-12-04 22:17

    I am using below in Angular to restrict character

    in HTML

    For Number Only

    <input
     type="text"
     id="score"
     (keypress) ="onInputChange($event,'[0-9]')"
     maxlength="3"
     class="form-control"> 
    

    for Alphabets Only

    <input
         type="text"
         id="state"
         (keypress) ="onInputChange($event,'[a-zA-Z]')"
         maxlength="3"
         class="form-control"> 
    

    In TypeScript

     onInputChange(event: any, inpPattern:string): void {   
            var input = event.key; 
            if(input.match(inpPattern)==null){
             event.preventDefault();
            }
          }
    
    0 讨论(0)
  • 2020-12-04 22:18

    This works in IE, Chrome AND Firefox:

    <input type="text" onkeypress="return event.charCode === 0 || /\d/.test(String.fromCharCode(event.charCode));" />
    
    0 讨论(0)
  • 2020-12-04 22:19
         .keypress(function(e)
                   {
                     var key_codes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 8];
    
                     if (!($.inArray(e.which, key_codes) >= 0)) {
                       e.preventDefault();
                     }
                   });
    

    You need Backspace and Delete keys too ;)

    0 讨论(0)
  • 2020-12-04 22:21

    The only event that contains information about the character typed is keypress. Anything character-related you may infer from the keyCode property of keydown or keyup events is unreliable and dependent on a particular keyboard mapping. The following will prevent non-numeric keyboard input all major browsers by using the character obtained from the keypress event. It won't prevent the user from pasting or dragging non-numeric text in.

    var input = document.getElementById("your_input");
    
    input.onkeypress = function(evt) {
        evt = evt || window.event;
        if (!evt.ctrlKey && !evt.metaKey && !evt.altKey) {
            var charCode = (typeof evt.which == "undefined") ? evt.keyCode : evt.which;
            if (charCode && !/\d/.test(String.fromCharCode(charCode))) {
                return false;
            }
        }
    };
    
    0 讨论(0)
  • 2020-12-04 22:21

    You can make changes to accept the keycode for Ctrl keys: 17, 18, 19, 20. Then your code will be like:

    function numbersonly(e, decimal) {
    var key;
    var keychar;
    
    if (window.event) 
        key = window.event.keyCode;
    else if (e) 
        key = e.which;
    else 
        return true;
    
    keychar = String.fromCharCode(key);
    
    if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) || (key==17) || (key==18) || (key==19) || (key==20))
       return true;     
    else if ((("0123456789").indexOf(keychar) > -1))
       return true;
    else if (decimal && (keychar == "."))
       return true;        
    else
       return false;
    }
    
    0 讨论(0)
提交回复
热议问题