Search code examples
javascriptknockout.jsknockout-validation

show validation message after user enters invalid value and focused out and then on keyup,using knockout validation


I am using knockout validation to validate the data before updating in db. I am not so familiar with knockout validation. "valueUpdate" in data-bind for an input box is with "afterkeydown", for which, each time i am giving some invalid value the message is coming up. But I want to show the message after user first time focused out from the input box. After that I want to show the message on key up. If I can set the valueUpdate after focus out from view model, this may help. Since I am having other bindings in data-bind, I can't just add data-bind attribute from vm. I checkedthis link.

Any idea how to do this?


Solution

  • Your fiddle example is using Knockout 2.3. If you use Knockout 3+ you can use the textInput binding.

    The textInput binding links a text box () or text area () with a viewmodel property, providing two-way updates between the viewmodel property and the element’s value. Unlike the value binding, textInput provides instant updates from the DOM for all types of user input, including autocomplete, drag-and-drop, and clipboard events.

    So instead of:

    <input data-bind='value: emailAddress,valueUpdate:"keyup"' required pattern="@"/>

    your binding would look like:

    <input data-bind='textInput: emailAddress' required pattern="@"/>

    Here is an updated fiddle: https://jsfiddle.net/mx45nnL6/2/