make an input only-numeric type on knockout

后端 未结 10 987
情书的邮戳
情书的邮戳 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:43

    An alternative approach: I have found that Knockout works well in combination with jQuery-validate. You just need to make sure that you validate the form before you attempt to use the numeric value.

    Say you have a form DOM element, you can set up validation rules via

    $(".yourform").validate({
        rules: {
            year: {
                digits: true,
                minlength: 4,
                maxlength: 4
            }
        },
        messages: {
            year: "Please enter four digits (e.g. 2009).",
        }
    });
    

    In your viewmodel you set the two-way binding up as usual, e.g. via self.year = ko.observable(""). Now make sure that you call $(".yourform").valid() before you are further processing self.year(). In my case, I am doing var year = parseInt(self.year(), 10). Right after form validation this is expected to always produce a meaningful result.

提交回复
热议问题