What is the correct way to filter numeric values ​​for a text field?

I am working on a text field that works with validation, which will not allow you to enter values ​​other than numeric values. So my initial code looked pretty simple and similar to this:

$(textField).onKeyPress(function(e) {
    if (e.which < 48 && e.which > 57)
        e.preventDefault();
});

It is quite complicated, but turns it (in the latest version of all browsers) Firefox will also do this by preventing movement with the arrow keys and the delete / return keys, while other browsers will not.

Looking around, I found that I would also need to check these keys and check the various properties found in e.

My last code looks something like this:

$(textField).onKeyPress(function(e) {
    var code = e.which || e.keyCode;
    if (code > 31  // is not a control key
        && (code < 37 || code > 40) // is not an arrow key
        && (code < 48 || code > 57) // is not numeric
        && (code != 46) // is not the delete key
    )
        e.preventDefault();
});

However, this seems too complicated to solve a rather simple problem, as it simply prevents a non-numeric number.

? ?

+3
3

. - , , . , . .

, . :

$("#textfield").on("keypress blur", function(e){
    if ( e.type === "keypress" )
        return !!String.fromCharCode(e.which).match(/^\d$/);
    this.value = this.value.replace(/[^\d].+/, "");
});

: http://jsfiddle.net/jonathansampson/S7VhV/5/

+4

http://jsfiddle.net/Pb2eR/23/ - Copy/Paste: http://jsfiddle.net/Pb2eR/47/ ( , , : )

http://jsfiddle.net/gpAUf/

.

: , , , - osx:)

: [1] (0-9) HTML jQuery?

$(".hulk").keyup(function(){    
    this.value = this.value.replace(/[^0-9\.]/g,'');    
});

HTML

<input type="text" class="hulk" value="" />

$(".hulk").keyup(function(){

    this.value = this.value.replace(/[^0-9\.]/g,'');

});

$(".hulk").bind('input propertychange', function() {

     this.value = this.value.replace(/[^0-9\.]/g,'');
});​

$(".hulk").bind('input propertychange', function(event) {
         if( !(event.keyCode == 8    // backspace
        || event.keyCode == 46      // delete
        || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
        || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
        || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
        ) {
            event.preventDefault();     // Prevent character input
    }
     this.value = this.value.replace(/[^0-9\.]/g,'');
});

+2

this will allow as int. it also deletes text if the user copies and pastes with the mouse.

$(document).ready(function () {
    $('#textfield').bind('keyup blur', function (e) {
        if (e.type == 'keyup') {
            if (parseInt($(this).val()) != $(this).val()) {
                $(this).val($(this).val().slice(0, $(this).val().length - 1));
            }
        } else if (e.type == 'blur') {
            $(this).val('');
        }
    });
});
-1
source

Source: https://habr.com/ru/post/1569534/


All Articles