在文字方面的新行字符增加在C#中的文本长度 [英] New line characters in text area increases text length in C#

查看:186
本文介绍了在文字方面的新行字符增加在C#中的文本长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net MVC应用程序有这个问题。

I have this problem in my asp.net mvc application.

在我的模型之一,有一个领域的说明。这个领域的数据库字段设置为为nvarchar(300)

In one of my model there is a field "Description". The database column for this fields is set to NVarchar(300).

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

In my view I am creating a a text area as following.

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

我使用jquery.validate.unobtrusive.min.js的客户端验证。因此,当在textarea的内容和长度的用户类型的推移它会显示消息超过300个字符请输入不超过300 characaters。

I am using "jquery.validate.unobtrusive.min.js" for client side validation. So when user types in the textarea and content length goes more than 300 characters it displays message "Please enter no more than 300 characaters."

一切工作正常,直到下sceanario来。
用户在文本区域输入以下数据。

Everything works fine till the following sceanario comes. User enters following data in the text area.

f
f
f
f
f
f
f
f
sdfa

(此内容有8个新行)

(this content has 8 new lines)

据不显眼的验证此内容的长度为300 (计数每个新行\\ n作为单个字符)这样的验证通过和页面回。

According to "unobtrusive" validation this content has length 300 (counting each new line "\n" as a single character) so the validation passes and page posts back.

在我的C#code,由于编码,同样的内容变得FO长度308 (计数每个新行\\ r \\ n为2个字符)这在燕鸥失败数据因为它基地操作只允许300个字符。

In my C# code, due to Encoding, the same content becomes fo length 308 (Counting each new line "\r\n" as 2 characters) which in tern fails the data base operation as it only allows 300 characters.

如果有人说我应该有 StringLength 属性,在这个特别的财产,我有以下原因没有这样的勇气。

If someone is saying that I should have StringLength attribute on this particular property, I have the following reason for not having it.

如果我把这个属性的客户端验证不发生这一特殊的属性,它关系到服务器,并因为该模型是无效它回来的页面错误信息。

If I put this attribute the client-side validation does not happen for this particular property, it goes to the server and since the model is not valid it comes back to the page with error message.

请告诉我这可能是这种情况的可能的解决方案?

Please advise me what could be the possible solution for this?

推荐答案

在通过@克里斯解决左看右看之后,我发现,这会导致比与<$ C $ textarea的其他任何控制死循环C> @maxlength 属性。
此外,我发现,使用(=传入验证textarea的值)将已经在开头和结尾的换行符切断,这意味着对数据库的操作还是当它试图挽救包含这些换行符文本失败。

因此,这里是我的解决方案:

After taking a closer look at the solution by @Chris, I found that this would cause an endless loop for any control other than textarea with the @maxlength attribute.
Furthermore, I found that using value (=the value of the textarea passed into the validator) would already have the leading and trailing line breaks cut off, which means the database operation still failed when it tried to save text containing those line breaks.
So here is my solution:

(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测试这一点,它为我工作得很好。

I tested this in Chrome and a few IE versions and it worked fine for me.

这篇关于在文字方面的新行字符增加在C#中的文本长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆