[英]How to prompt a message in textarea if input is more than 200 characters
How to prompt an error message in textarea if input is more than 200 characters. 如果输入超过200个字符,如何在文本区域中提示错误消息。 I am trying maxLength attribute. 我正在尝试maxLength属性。 But it stops taking input. 但是它停止接受输入。 I dont want that. 我不想要那个。 I want an error message there 我想要一个错误消息
Try writing a Jquery function which is called on the input change using the onchange attribute. 尝试编写一个使用onchange属性在输入更改上调用的Jquery函数。 Then use var messageTxt = $("#yourelementid").value(); If(messageTxt.length() > 200) { alert("input is more than 200 characters"); }
然后使用var messageTxt = $("#yourelementid").value(); If(messageTxt.length() > 200) { alert("input is more than 200 characters"); }
var messageTxt = $("#yourelementid").value(); If(messageTxt.length() > 200) { alert("input is more than 200 characters"); }
<style type="text/css">
.input-error ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: red;
}
.input-error :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: red;
opacity: 1;
}
.input-error ::-moz-placeholder { /* Mozilla Firefox 19+ */
color: red;
opacity: 1;
}
.input-error :-ms-input-placeholder { /* Internet Explorer 10-11 */
color: red;
}
</style>
<div class="text-wrapper">
<textarea id="infoText" placeholder="Type your text"></textarea>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#infoText').bind('input propertychange', function() {
if($(this).val().length>4){
$(this).parent(".text-wrapper").addClass("input-error");
$(this).attr("placeholder","Text cannot exceed more than 200 characters")
$(this).val("");
}
else{
$(this).parent(".text-wrapper").removeClass("input-error")
} ;
});
});
</script>
since your post has an angular tag, I am assuming you are using angular. 由于您的帖子带有角度标签,因此我假设您使用的是角度标签。 In general, you can put a watch on the textarea model. 通常,您可以在textarea模型上放置手表。
$scope.$watch('yourTextAreaModel', function(newVal) {
if(newVal.length > 200){
alert("input cannot be more than 200 characters");
}
})
*You might not even need a watch but it depends on your application. *您甚至可能不需要手表,但这取决于您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.