Search code examples
javascriptknockout.jsknockout-validation

Enter needs to be pressed twice to trigger, KnockoutJS with knockout.validation.js


I'm building a form-page and I got stuck when I wanted to implement an "enter-key" function to trigger the validation and the method at the same time.

Here's a JS-Fiddle of the example

As you can tell, you need to press Enter, twice, in order for the method to trigger. I believe knockout.validation has it's own event binding, and maybe that's why

<input type="text" data-bind="event: {'keypress': enterKey}, value: customer.telephone">
<input type="button" data-bind="click: sendCustomer" value="send">

enterKey = function (d, e) {
        if (e.keyCode == 13) {
            alert("enter has been pressed..");
            sendCustomer();
        }
        return true;
    }

Solution

  • There is a difference between keypress & keyup. This link can be useful.

    So I have replaced the keypress with keyup

    <input type="text" data-bind="event: {'keyup': enterKey}, value: customer.telephone">
    

    and it seems to be working.Hope this will be helpful to you.

    JSFIDDLE