【问题标题】:New line characters in text area increases text length in C#文本区域中的换行符增加 C# 中的文本长度
【发布时间】:2012-06-29 18:18:15
【问题描述】:

我在我的 asp.net mvc 应用程序中遇到了这个问题。

在我的一个模型中,有一个“描述”字段。该字段的数据库列设置为NVarchar(300)

在我看来,我正在创建一个如下的文本区域。

@Html.TextAreaFor(m => m.Description, new { maxlength = "300" })

我正在使用“jquery.validate.unobtrusive.min.js”进行客户端验证。因此,当用户在文本区域中键入并且内容长度超过 300 个字符时,它会显示消息“请输入不超过 300 个字符”。

在以下场景出现之前一切正常。 用户在文本区域输入以下数据。

f
f
f
f
f
f
f
f
sdfa

(此内容有8个新行)

根据“不显眼”验证,此内容的长度为 300(将每个新行“\n”计为单个字符)因此验证通过并返回页面。

在我的 C# 代码中,由于编码,相同的内容变成 fo 长度 308 (将每个新行“\r\n”计为 2 个字符) 这在 tern 数据库操作失败为它只允许 300 个字符。

如果有人说我应该在这个特定属性上具有StringLength 属性,我有以下原因没有它。

如果我设置此属性,则不会针对此特定属性进行客户端验证,它会转到服务器,由于模型无效,它会返回页面并显示错误消息。

请告诉我可能的解决方案是什么?

【问题讨论】:

    标签: javascript asp.net-mvc validation textarea newline


    【解决方案1】:

    在仔细研究了@Chris 的解决方案后,我发现这将导致除 textarea 之外的任何控件的无限循环,该控件具有@maxlength 属性。
    此外,我发现使用value(=传递给验证器的文本区域的值)已经将前导和尾随换行符切断,这意味着数据库操作在尝试保存包含的文本时仍然失败那些换行符。
    所以这是我的解决方案:

    (function ($) {
        if ($.validator) {
            //get the reference to the original function into a local variable
            var _getLength = $.validator.prototype.getLength;
    
            //overwrite existing getLength of validator
            $.validator.prototype.getLength = function (value, element) {
    
                //double count line breaks for textareas only
                if (element.nodeName.toLowerCase() === 'textarea') {
    
                    //Counts all the newline characters (\r = return for macs, \r\n for Windows, \n for Linux/unix)
                    var newLineCharacterRegexMatch = /\r?\n|\r/g;
    
                    //use [element.value] rather than [value] since I found that the value passed in does cut off leading and trailing line breaks.
                    if (element.value) {
    
                        //count newline characters
                        var regexResult = element.value.match(newLineCharacterRegexMatch);
                        var newLineCount = regexResult ? regexResult.length : 0;
    
                        //replace newline characters with nothing
                        var replacedValue = element.value.replace(newLineCharacterRegexMatch, "");
    
                        //return the length of text without newline characters + doubled newline character count
                        return replacedValue.length + (newLineCount * 2);
                    } else {
                        return 0;
                    }
                }
                //call the original function reference with apply
                return _getLength.apply(this, arguments);
            };
        }
    })(jQuery);
    

    我在 Chrome 和几个 IE 版本中对此进行了测试,对我来说效果很好。

    【讨论】:

      【解决方案2】:

      在包含 jquery.validate.js 后,您可以通过将以下内容添加到您的 javascript 中来更改客户端验证中 getLength 的行为以重复计算换行符。这将导致服务器端和客户端长度方法匹配,让您使用 StringLength 属性(我假设您对 StringLength 的问题是服务器和客户端验证方法不同)。

      $.validator.prototype._getLength = $.validator.prototype.getLength;
      $.validator.prototype.getLength = function (value, element) {
      // Double count newlines in a textarea because they'll be turned into \r\n by the server. 
          if (element.nodeName.toLowerCase() === 'textarea')
              return value.length + value.split('\n').length - 1;
          return this._getLength(value, element);
      };
      

      【讨论】:

      • 拆分对我来说不太奏效。我遇到了尾随换行符的问题,使用您的方法没有计算在内。我改用这个://replaces newline characters with 2 spaces to make the count match the server's. var replacedValue = value.replace(/(\n)/gm, " "); return replacedValue.length; 注意:代码块不知何故只显示了一个应该有两个的空格。
      猜你喜欢
      • 2017-09-06
      • 2014-12-14
      • 1970-01-01
      • 2023-03-14
      • 2018-06-21
      • 1970-01-01
      相关资源
      最近更新 更多