在C#中,文本区域中的换行符会增加文本长度。

10

我在我的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

根据“不显眼”的验证,此内容长度为300(将每个新行“\n”视为单个字符),因此验证通过并且回传页面。在我的C#代码中,由于编码的原因,相同的内容变成了308的长度(将每个新行“\r\n”视为2个字符),这导致数据操作失败,因为它仅允许300个字符。如果有人说我应该在此特定属性上使用"StringLength"属性,我有以下原因没有使用它。如果我添加此属性,则客户端验证不会发生在此特定属性上,而是转到服务器,由于模型无效,它带着错误消息返回到页面。请告诉我可能的解决方案?
2个回答

7

仔细研究了@Chris的解决方案后,我发现这会对除了带有@maxlength属性的textarea之外的任何控件都造成无限循环。
此外,我发现使用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版本中测试了这个,对我来说都正常工作。


4

您可以通过在包含jquery.validate.js之后添加以下内容来更改客户端验证中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);
};

1
分割并没有完全奏效。我遇到了一个问题,尾随的换行符没有被计算在你的方法中。我使用了这个: //将换行符替换为2个空格以使计数与服务器匹配。 var replacedValue = value.replace(/(\n)/gm, " "); return replacedValue.length;注意:代码块中某些地方显示了一个空格,但实际上应该是两个空格。 - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接