最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trying to remove non numeric characters from textbox using jQuery - Stack Overflow

programmeradmin0浏览0评论

I'm trying to remove a character as it's typed/keyed up to test whether it's a number or not, if it is a number, keep it, if not, remove it. It can be more than one digit. for example, I type in a "d", it should be deleted from the textbox. I then type a "1", where it should stay in the textbox. I then type a 4 and it also stays so the textbox now reads "14". Then I type "g", which should be deleted. Then I type a "5" and now the text box reads 145. This is what I have so far.

   $("#txtTestNumbersOnlyRegex").keyup(function () {

            $("#txtTestNumbersOnlyRegex").val(function (index, value) {
                var keyPressed = value.substr(0, value.length - 1);
                var regEx = new RegExp("/^5$/");
                if(!regEx.test(keyPressed)) {
                    alert("true");
                    return value.substr(0, value.length - 1);
                }                   
            });      

        });            

I'm trying to remove a character as it's typed/keyed up to test whether it's a number or not, if it is a number, keep it, if not, remove it. It can be more than one digit. for example, I type in a "d", it should be deleted from the textbox. I then type a "1", where it should stay in the textbox. I then type a 4 and it also stays so the textbox now reads "14". Then I type "g", which should be deleted. Then I type a "5" and now the text box reads 145. This is what I have so far.

   $("#txtTestNumbersOnlyRegex").keyup(function () {

            $("#txtTestNumbersOnlyRegex").val(function (index, value) {
                var keyPressed = value.substr(0, value.length - 1);
                var regEx = new RegExp("/^5$/");
                if(!regEx.test(keyPressed)) {
                    alert("true");
                    return value.substr(0, value.length - 1);
                }                   
            });      

        });            
Share Improve this question asked Aug 11, 2015 at 1:00 ddeamaralddeamaral 1,4434 gold badges29 silver badges43 bronze badges 1
  • use keypress check my snippet below: You can intercept the values that are ing in and decide what to do, So it is better to have the keypress else your text gets replaced everytime. For example say if you are in middle of input val and you are editing your caret will jump to the end in other cases, since the whole text is getting replaced – Joy Biswas Commented Aug 11, 2015 at 1:21
Add a ment  | 

3 Answers 3

Reset to default 4

You might want to try this:

$("#txtTestNumbersOnlyRegex").keyup(function () { 
    var newValue = $(this).val().replace(/[^0-9]/g,'');
    $(this).val(newValue);    
 });  

You can try it here https://fiddle.jshell/aooh0gkz/

You can use regex to replace any non digits with empty space, although this will look a bit weird.

$('input').keyup(function() {
    $(this).val($(this).val().replace(/\D/, ''));
});

Full snippet:

$('input').keydown(function() {
        console.log($(this).val());
        $(this).val($(this).val().replace(/\D/, ''));
    });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">


Or you could use input type="number" if you don't care about IE support.

You can intercept the values that are ing in and decide what to do, So it is better to have the keypress else your text gets replaced everytime and say if you are in middle of input val and you are editing caret will jump to the end in other cases

$("#txtTestNumbersOnlyRegex").keypress(function (event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if((keyCode>=65 && keyCode<=90) || (keyCode>=97 && keyCode<=122))
        return false;
    else return true;
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=text id = txtTestNumbersOnlyRegex />

发布评论

评论列表(0)

  1. 暂无评论