2016-08-30 7 views
0

個々の文字の代わりにテキスト領域の単語を数えたいと思います。MVCの単語の制限

モデルクラス

[RegularExpression(@"[^<>]*", ErrorMessage = "Invalid entry"), StringLength(200)] 
     public string Day1Journal { get; set; } 

HTML

@Html.TextAreaFor(m => m.StudentJournaldtls.Day1Journal, IsReadOnly == true || IsSubmitted == true ? (object)new { col = 2, @class = "CharacterLimit required", @maxlength = 200, @readonly = "readonly" } : new { col = 2, @class = "CharacterLimit required", @maxlength = 200 }) 
          <small>No of characters left:<span class="charsLeft"></span></small> 

Javascriptを

<script type="text/javascript"> 
    $(function() { 

$(".CharacterLimit").each(function (i, t) { 
    var charsLeft = $(this).attr("maxlength") - $(this).val().split(' ').length 
    $(this).parent().find(".charsLeft").text(charsLeft); 
}); 

$(".CharacterLimit").keyup(function (e) { 
    var charsLeft = $(this).attr("maxlength") - $(this).val().split(' ').length 
    $(this).parent().find(".charsLeft").text(charsLeft); 
}); 

</script> 

答えて

3

あなたは言葉は次のように、空白で値をsplitingことにより、テキストでカウントを取得することができます

$("textbox").val().split(' ').length 
+0

こんにちは、私は言葉の数を得ることができました。しかし、単語の制限はまだ200文字で停止します。 – user3807187

関連する問題