2017-05-08 74 views
0

如果輸入超過200個字符,如何在textarea中提示錯誤消息。 我正在嘗試maxLength屬性。但它停止採取投入。我不想那樣。我想要一個錯誤消息如果輸入超過200個字符,如何在textarea中提示消息

+1

歡迎SO。請發佈演示您嘗試完成的代碼,您嘗試過的以及失敗的地方。 –

+0

我們可以使用jquery進行此操作 – Ram

+0

這是關於Angular還是AngularJS? –

回答

2

嘗試編寫一個使用onchange屬性在輸入更改上調用的Jquery函數。 然後使用 var messageTxt = $("#yourelementid").value(); If(messageTxt.length() > 200) { alert("input is more than 200 characters"); }

+0

我正在使用Angular 2.你能告訴我在這方面 – surbhiGoel

0

,因爲您的文章有棱角的標籤,我假設你正在使用的角度。一般來說,您可以在textarea模型上放置手錶。

$scope.$watch('yourTextAreaModel', function(newVal) { 
    if(newVal.length > 200){ 
    alert("input cannot be more than 200 characters"); 
    } 
}) 

*您甚至可能不需要手錶,但它取決於您的應用程序。

+1

有沒有使用手錶的情況。在這個例子中,使用'ng-change'作爲angularjs,'ngModelChange'作爲角度 – tic

+0

我認爲OP使用的是角度1.x.是的angular2或角度,ngOnChanges也將工作 –

+0

謝謝。我得到了我的回答 – surbhiGoel

2
<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> 

+0

試試這個,我希望這是你想要的 –

相關問題