make an input only-numeric type on knockout

后端 未结 10 1065
情书的邮戳
情书的邮戳 2020-11-30 06:15

i read many tutorials but i dont know how to do this, this is the input

input(type=\"text\",name=\"price\",id=\"price\"data-bind=\"text: price,valueUpdate:[\         


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 06:21

    I had a similar problem.

    I also needed to ensure inter values only, and for IE9 and up (so type=numberical was not enough), and since we have a lot of international customers, i could not rely on keycodes either, so the following is what i ended up with:

    //In my js class method (self is this as usual)
    self.ensureNumberical = function (data, e) {
        var keyValue = e.key;
        if (keyValue.match(/[0-9]/g)) {
            return true;
        }
        return false;
    }
    
    //In my MVC View
    data-bind="event: { keypress: ensureNumberical }"
    

提交回复
热议问题