<div class="col-lg-6 col-sm-6">
<div class="form-group">
<label>培训时长(单位:小时)</label>
<!-- <input type="text" class="form-control" id="person_train_form_timelenth" name="timelenth" placeholder="培训时长" /> -->
<input type="number" class="form-control" id="person_train_form_timelenth" name="timelenth" placeholder="培训时长" min="0" max="100" step="1"/>
</div>
</div>
只能输入整数,如果要输入浮点数呢,可以通过max
、min
和step
去定义。
type="number" 数字类型
min="0" 最小值
max="100" 最大值
step="1" 点击右边上下按钮每次增加或减少的值
<input type="number" min="0" max="100" />
<input type="number" min="0.00" max="100.00" step="0.01" />
-------------------------------------------------------------------------------------------------------------------------
解决input type='number'类型可以输入e
当给 input 设置类型为 number 时,比如,我想限制,只能输入 0-9 的正整数,正则表达式如下:
/^[0-9]?$/ // 匹配 0-9 的整数且只匹配 0 次或 1 次
用正则测试,小数点是无法通过的,且因为 设置了类型为 number,也无法输入字母,
但实际上,1. 类似这样的不合规则的数字是可以通过验证的,且可以键入字母 e,因为 e 在数学上代表2.71828,也是一个数字。
解决办法如下:
<input type='number' οnkeypress='return( /[\d]/.test(String.fromCharCode(event.keyCode) ) )' />
实际测试失败,依然可以输入e