简体   繁体   English

如何在淘汰赛在输入栏上进行在线验证

[英]How to perform inline validation in Knockout on an input field

I want to allow the user to only enter postive numbers and numbers less then 100. How can i modify this input markup to get my required validation. 我想允许用户仅输入正数和小于100的数字。我如何修改此输入标记以获得所需的验证。

<input data-bind="value : $root.rootData.Page" class="form-control">

Edit: I know i can validate in JS, but for my particular case i want to do it solely in the makrup. 编辑:我知道我可以在JS中进行验证,但是对于我的特殊情况,我只想在makrup中进行验证。

You can use regular expressions with HTML5 using the pattern attribute. 您可以使用pattern属性在HTML5中使用正则表达式。

How can I validate number between 1 and 99 using regular expression? 如何使用正则表达式验证1到99之间的数字? - Will give you some information about different ways to check if a number is between 1 and 99 -将为您提供一些有关检查数字是否在1到99之间的不同方法的信息

From your example, you could use the following 从您的示例中,您可以使用以下内容

<input data-bind="value : $root.rootData.Page" class="form-control" pattern="^[1-9][0-9]?$">

instead of using regex to match a number field, why not just use the number type and provide min/max? 为何不使用正则表达式来匹配数字字段,为什么不只使用数字类型并提供最小/最大?

<input type="number" min="1" max="99" step="1" data-bind="value : $root.rootData.Page" class="form-control" />

Regex could be used in combination with number type as a fallback, since FF support for the number type is lacking: 由于缺少对数字类型的FF支持,因此可以将正则表达式与数字类型结合使用作为后备方法:

<input type="number" min="1" max="99" step="1" pattern="^[1-9][0-9]?$" data-bind="value : $root.rootData.Page" class="form-control" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM