我需要限制用户输入长度指定的列中唯一的整数。
onChanged
上面的方法适用于UI中的字符串类型的字段,但是当我添加数字以限制字母数字输入时,则最大长度不起作用。
期望将输入限制为最多4个字符,并且只能为正数输入
答案 0 :(得分:0)
如果您的“值”属性是整数,则无需以简单形式指定“ 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
然后在您的视图中将其应用(假设未禁用涡轮链接):
$( document ).on('turbolinks:load', function() {
$('#input-id-here').mask('0000');
})