当用户尝试超过输入字段的最大长度时,如何向用户返回错误消息?
这是我在haml中使用的代码。
= f.input :title, placeholder: 'Enter Title', :input_html => { :class => "form-control", autocomplete: :off, :maxlength => 255 }
我在这里使用了 maxlength 并且它工作正常,但我想在输入字段下方显示一个错误,即“您已达到最大字符长度”。
有什么办法可以完成这个任务吗?
任何形式的帮助将不胜感激。
您可以使用input
触发或keyup
JQUERY
$( document ).ready(function() {
//make sure name attribute is the same
$("input[name='title']").on('keyup', function() {
if($(this).val().length > 255) {
$(this).css({"color": "red"})
}else{
$(this).removeAttr( "style" )
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句