0

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;
    }
Salmin Skenderovic
  • 1,750
  • 9
  • 22
  • Please include a full but minimal repro *in the question itself* too. That way if the link rots or jsfiddle is down your question will still be useful to future visitors with the same problem. – Jeroen Nov 11 '15 at 09:45

1 Answers1

1

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

Community
  • 1
  • 1
brk
  • 48,835
  • 10
  • 56
  • 78
  • Thanks! For some reason I'm getting the old solution on your link (with keypress). Maybe it's cached on my phone. I tried chaninging it tho and it worked – Salmin Skenderovic Nov 11 '15 at 18:07