HTML text input allow only numeric input

前端 未结 30 3423
孤街浪徒
孤街浪徒 2020-11-21 04:58

Is there a quick way to set an HTML text input () to only allow numeric keystrokes (plus \'.\')?

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 05:20

    A easy way to resolve this problem is implementing a jQuery function to validate with regex the charaters typed in the textbox for example:

    Your html code:

    
    

    And the js function using jQuery

    $(function() {
        $('.integerInput').on('input', function() {
          this.value = this.value
            .replace(/[^\d]/g, '');// numbers and decimals only
    
        });
    });
    

    $(function() {
            $('.integerInput').on('input', function() {
              this.value = this.value
                .replace(/[^\d]/g, '');// numbers and decimals only
                
            });
        });
    
    
    
    
    
    		

提交回复
热议问题