MVC字数限制

本文关键字:MVC | 更新日期: 2023-09-27 18:04:42

我想计算文本区域中的单词数,而不是每个字符。

模型类

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

MVC字数限制

您可以通过用空格分隔值来获得文本中的字数,如:

$("textbox").val().split(' ').length
相关文章:
  • 没有找到相关文章