我正在使用表单验证,并希望使用模式属性来验证输入字段。该字段具有以下标准:
那么应该是什么样的确切模式RegEx
。我尝试使用以下代码但是如何实现百分比条件?
<input type="text"
pattern="[0-9]+([\.][0-9]{0,2})?"
title="This must be a number with up to 2 decimal places and/or %">
答案 0 :(得分:3)
你快到了。将%?
添加到您的模式中:
input:invalid {
color: red;
}
&#13;
<input type="text"
pattern="[0-9]+(\.[0-9]{0,2})?%?"
title="This must be a number with up to 2 decimal places and/or %">
&#13;