簡體   English   中英

文本區域中的換行符增加了C#中的文本長度

[英]New line characters in text area increases text length in C#

我在asp.net mvc應用程序中遇到此問題。

在我的一個模型中有一個字段“描述”。 此字段的數據庫列設置為NVarchar(300)

在我看來,我正在創建一個文本區域如下。

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

我正在使用“jquery.validate.unobtrusive.min.js”進行客戶端驗證。 因此,當textarea中的用戶類型和內容長度超過300個字符時,它會顯示消息“請輸入不超過300個字符”。

一切正常,直到下面的情景來臨。 用戶在文本區域中輸入以下數據。

f
f
f
f
f
f
f
f
sdfa

(此內容有8條新線)

根據“不顯眼”驗證,此內容的長度為300 (將每個新行“\\ n”計為單個字符),以便驗證通過並返回頁面帖子。

在我的C#代碼中,由於編碼,相同的內容變為長度308 (將每個新行“\\ r \\ n”計為2個字符) ,這使得數據庫操作失敗,因為它只允許300個字符。

如果有人說我應該在這個特定的屬性上有StringLength屬性,我有以下原因沒有它。

如果我將此屬性設置為此特定屬性不會發生客戶端驗證,則會轉到服務器,因為模型無效,所以它會返回到包含錯誤消息的頁面。

請告訴我可能的解決方案是什么?

在仔細研究了@Chris的解決方案之后,我發現除了帶有@maxlength屬性的textarea之外,這將導致無限循環。
此外,我發現使用value (=傳遞給驗證器的textarea的值)已經將前導和尾部換行符切斷,這意味着當它嘗試保存包含這些換行符的文本時數據庫操作仍然失敗。
所以這是我的解決方案:

(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版本中對此進行了測試,它對我來說很好。

您可以通過在包含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);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM