New line characters in text area increases text length in C#

后端 未结 2 782
无人及你
无人及你 2021-01-04 19:37

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

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

2条回答
  •  盖世英雄少女心
    2021-01-04 19:50

    You can change the behavior for getLength in client validation to double count newlines by adding the following to your javascript after you've included jquery.validate.js. This will cause the server-side and client-side length methods to match letting you use the StringLength attribute (I assume your issue with StringLength was that the server and client validation methods differed).

    $.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);
    };
    

提交回复
热议问题