当每行包含来自textarea的50个字符时,如何保留换行符?

时间:2017-03-24 06:22:54

标签: jquery asp.net-mvc

我正在研究MVC 5,我有一个textarea控件,2行文本 - 最多100个字符 - 每行最多50个字符。

查看,

@Html.TextAreaFor(model => model.AdImageText, new { @id = "idAdImageText", @class = "form-control input-sm ", @style = "height: 55px !important;", @maxlength = "100"})

我已经应用了以下逻辑,但它没有工作电流。有时候$("#idAdImageText").val();包含102个字符。

debugger;
        if ($("#txtfileTitle").attr("placeholder") != "" && $("#txtfileTitle").attr("placeholder") != "Upload Image" && $("#idAdImageText").val() != "" && $("#idHyperLink").val() != "") {
            var str = $("#idAdImageText").val();/* textarea should have maximum 50 characters.*/
            var result = '';

            var strSplitStrings = [];
            while (str.length > 0) {
                result = str.substring(0, 50) + '\n';
                strSplitStrings.push(result);
                str = str.substring(50);
               
            }

            if (strSplitStrings.length == 1) {
                var message = "2 Line Text – Maximum 100 characters - each line should have maximum 50 characters.";
                errormesssage(message);
            }
            else if (strSplitStrings.length == 2) {
                if (strSplitStrings[0].length == 50 && strSplitStrings[1].length == 50) {

                    $('#frmAdvertisement').submit();

                }
                else {
                    var message = "2 Line Text – Maximum 100 characters - each line should have maximum 50 characters.";
                    errormesssage(message);
                }
            }
        }
        else {

            var message = "All mandatory fields are not entered which is marked with a star";
            errormesssage(message);
        }

我该怎么做?请帮帮我......

1 个答案:

答案 0 :(得分:0)

更改了这样的代码,

 var str = $("#idAdImageText").val();
            var result = '';

            var strSplitStrings = [];
            while (str.length > 0) {
                result = str.substring(0, 50) + '\n';
                strSplitStrings.push(result.substring(0,result.length-1));
                str = str.substring(50);               
            }           

现在运作良好!