2016-08-30 135 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

你可以得到詞語的文本數由劈裂用空格的值,如:

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

嗨,我能夠得到字數。但單詞限制仍然停留在200個字符。 – user3807187