我需要限制用户使用长度规范为我的一列输入唯一的整数。
<%= f.input :value, as: :numeric, :input_html => { :maxlength => 4 } %>
上面的方法适用于UI中字段的字符串类型,但当我添加数字来限制字母数字输入时,最大长度不起作用。
期望将输入限制为最多4个字符,并且只能是正数输入
发布于 2020-07-29 21:34:06
如果你的"value“属性是一个整数,你不需要在simpleform中指定" as”,因为它会自动为你处理:
<%= f.input :value, as: :integer, input_html: { min: '0', max: '9999', step: '1' } %>
#If you want decimals you can change :step to 'any'我个人的偏好是使用jquery-mask:https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html
在你的视图中,你可以像这样应用它(假设turbolinks没有被禁用):
$( document ).on('turbolinks:load', function() {
$('#input-id-here').mask('0000');
})https://stackoverflow.com/questions/63153971
复制相似问题