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

javascript - Prevent "DOT" for Input type number in Mobile Devices - Stack Overflow

programmeradmin1浏览0评论

am working With jQueryMobile with PhoneGap.. Currently I have an issue.. I search about it but didnt get an exact reply. My problem is i want to prevent the typing DOT in Textbox. Textbox is for enter AGE.

<input type="number" class="form_textbox_half" data-role="none" id="age" pattern="[0-9]*">

And Its Keypress event ;

$('#age').live('keypress', function(){preventDot(event)});

Then its Function is

function preventDot(event) {
    alert(0);
    //var test = document.getElementById("#age");
    var key = event.charCode ? event.charCode : event.keyCode;
    $("#age").innerHTML = key;
    if (key == 46) {
        return false;
    }
}

But its not works.. I want to prevent typing DOT in the textbox. I think its a small issue.. :( But didnt get a solution for it.. please help me!

am working With jQueryMobile with PhoneGap.. Currently I have an issue.. I search about it but didnt get an exact reply. My problem is i want to prevent the typing DOT in Textbox. Textbox is for enter AGE.

<input type="number" class="form_textbox_half" data-role="none" id="age" pattern="[0-9]*">

And Its Keypress event ;

$('#age').live('keypress', function(){preventDot(event)});

Then its Function is

function preventDot(event) {
    alert(0);
    //var test = document.getElementById("#age");
    var key = event.charCode ? event.charCode : event.keyCode;
    $("#age").innerHTML = key;
    if (key == 46) {
        return false;
    }
}

But its not works.. I want to prevent typing DOT in the textbox. I think its a small issue.. :( But didnt get a solution for it.. please help me!

Share Improve this question edited Jun 14, 2013 at 9:16 Adil Shaikh 44.7k17 gold badges95 silver badges112 bronze badges asked Jun 14, 2013 at 9:14 ULLAS MOHAN.VULLAS MOHAN.V 1,5312 gold badges19 silver badges36 bronze badges 1
  • Try this solution that I've posted recently. – Lentyai Commented Dec 19, 2016 at 16:45
Add a ment  | 

4 Answers 4

Reset to default 2

Fixed your issue

FIDDLE

The problem was that you were not using preventDefault to stop the propogation of the keypress event

This is how the modified method looks:

function preventDot(event)
{
    alert(event.charCode)
    //var test = document.getElementById("#age");
    var key = event.charCode ? event.charCode : event.keyCode;  
    $("#age").innerHTML = key;
    if (key == 46)
    {
        event.preventDefault();
        return false;
    }    
}

I found only this to work

$(".vinput").on("input change paste",
function filterNumericAndDecimal(event) {
    var formControl;
    formControl = $(event.target);
    var newtext = formControl.val().replace(/[^0-9]+/g, "");
    formControl.val(''); //without this the DOT wont go away on my phone!
    formControl.val(newtext);
});

but if you need the dot included use [^0-9.] as regex

you can try this code i solve with this currentlly

function bindOnInputQuantity() {    
    $('.receiveQty').blur('input', function() {
    var sel = $(this);
    var selDel = $(this).next();
    if(sel.val() === '') {
        sel.val(0);
        sel.data('current', 0);
    } else {
        if(parseInt(sel.val()) > parseInt(selDel.val())) {
            alert('Receive Quantity Cannot More Then Delivery Quantity!');
            sel.val(sel.data('current'));
            sel.focus();
            return false;
        } else {
            sel.val(parseInt(sel.val()));
            sel.data('current', sel.val());
        }            
      }        
   });}                                                                            

Use like this

$('#age').live('keypress', function(event){preventDot(event)});

See Demo

发布评论

评论列表(0)

  1. 暂无评论