Multiline textbox value split using javascript in Asp.Net C#

Tags: , ,



Asp.Net C# Multiline textbox(textbox1) value split after 35 charaters of address & add to textbox(textbox2 – Singleline) again split next 35 charaters & add to textbox(textbox3 – Singleline) again split next 35 charaters & add to textbox(textbox4 – Singleline) again split next 35 charaters & add to textbox(textbox5 – Singleline). Note: While Splitting value after 35 characters make sure if a word is incomplete then add that word in next line like(…31 address – here at add position of address is 35 characters so it’ll get slipt & gets add to new textbox, but I want that word address in new textbox & from their it should count to 35 charaters like so on.)

I’ve tried this but didn’t work..

function CheckReturns() {

            var txt = document.getElementById("TextBox1");
            var splitResults = txt.value.split("n");

            if (splitResults[splitResults.length - 1].length < 35) {
                if (splitResults[3].length > 0) {
                    document.getElementById('address4').value = splitResults[3];
                }
                return true;
            }
            else {
                document.getElementById('address1').value = splitResults[0];
                document.getElementById('address2').value = splitResults[1];
                document.getElementById('address3').value = splitResults[2]; 
                txt.value = txt.value + "n";
            }
        }
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" MaxLength="140" Rows="10" Columns="60"
                onblur="return CheckReturns();" placeholder="Enter Address here.."></asp:TextBox>
            <br />
            Address1:<asp:TextBox ID="address1" runat="server" Width="220px" ></asp:TextBox><br />
            <br />
            Address2:<asp:TextBox ID="address2" runat="server" Width="220px" ></asp:TextBox><br />
            <br />
            Address3:<asp:TextBox ID="address3" runat="server" Width="220px" ></asp:TextBox><br />
            <br />
            Address4:<asp:TextBox ID="address4" runat="server" Width="220px" ></asp:TextBox>
Please help.

Answer

Here’s a JS that implements the logic I discussed in my first comment:

let longAddr = "this is a really long address with lots of words longer than 35 characters all over the place and i dont know where it is going to stop it just keeps going so it's like the longest address in the world which is a little bit crazy but there you go";

let addr = ["","","","",""];

for(let i = 0; i < 5 && longAddr.length > 0; i++){

    if(longAddr.length < 35 || i == 4)
    {
        addr[i] = longAddr;
        break;
    }
    
    let cut = 35;
    while(longAddr[cut] !== " " && cut > 0)
        cut--;
    if(cut == 0) //word longer than 35 chars here
      cut = 35;
    
    addr[i] = longAddr.slice(0, cut);
    longAddr = longAddr.slice(cut + 1);
}

console.log(addr);

It results in an array that has 5 elements with the string cut up into them. Now all you have to do is put the values in your textboxes



Source: stackoverflow