在用户输入无效值后显示验证消息并聚焦然后在 keyup 上,使用敲除验证

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

我在更新数据库之前使用敲除验证来验证数据。 我对敲除验证不太熟悉。 输入框的数据绑定中的 "valueUpdate" 与 "afterkeydown" 一起使用,为此,每次我给出一些无效值时都会出现消息。 但是我想在用户第一次从输入框中聚焦后显示消息。之后我想在按键上显示消息。 如果我可以在从视图模型中移出焦点后设置 valueUpdate,这可能会有所帮助。 因为我在数据绑定中有其他绑定,所以我不能只从 vm 添加数据绑定属性。 我查了this link.

知道怎么做吗?

您的 fiddle 示例使用的是 Knockout 2.3。如果您使用 Knockout 3+,则可以使用 textInput 绑定。​​

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.

所以代替:

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

你的绑定看起来像:

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

这是更新后的 fiddle: https://jsfiddle.net/mx45nnL6/2/