How to split a period separated string in javascript

Hello Experts!

I have to query a table that pulls back ip address information (i.e. X.X.X.X - where X is a number). I need to be able to split this up and put each "X" in a different textbox. How do I break apart and individualize the numbers?

The old code already has this information (just so you can see what I'm working with).

 <WebMethod()> _
    Public Function GetSubNet(ByVal GateWay As String) As String
        Dim Subnet As String

        Subnet = GateWay.Substring(0, GateWay.LastIndexOf(".") + 1)

        Return Subnet

    End Function

Thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Well splitting in javascript is easy. You use the split() function.

Assume you have 4 text boxes with IDs of text1, text2, text3, and text4

function setTextBoxes(psMyIp)
  sArr = psMyIp.split(".");

  document.getElementById("text1").value = sArr[0];
  document.getElementById("text2").value = sArr[1];
  document.getElementById("text3").value = sArr[2];
  document.getElementById("text4").value = sArr[3];

Open in new window

You could put it in a loop but this will never be more than 4 so it isn't worth it.

Is this what you're after?
Lee SavidgeCommented:
In my last post, you will need to pass in the IP address to the function.
CementTruckAuthor Commented:
@Lee Savidge,

The example code I posted resides on a javascript.vb page that gets called by many different pages. What I need is just a way to split the ip address up and have it ready for use but not insert the values in a textbox. Maybe dim 4 strings and add the individual values?

would this work?

sArr = psMyIp.split(".");

Dim ip1 As String
Dim ip2 As String
Dim ip3 As String
Dim ip4 As String

ip1  = sArr[0];
ip2  = sArr[1];
ip3  = sArr[2];
ip4  = sArr[3];

Your example would work if the javascript was on the same page that textboxes are in.

Kind of a noob to javascript. Please forgive the explanation.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Lee SavidgeCommented:
Javascript is client side so it only exists within the page on the client. If this is for multiple pages, then you need to store it somewhere serverside in memory for use elsewhere. Javascript may not be what you want to use.

The code I posted will split it but the IP octets will need to be stored either in a page variable or posted back to the server to use. If you're posting back you might as well do the split server side.

I'd need more details to help you much further.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CementTruckAuthor Commented:
In trying to be more efficient we created a page that houses all the repetetive code that we use all over the site. We call the javascript function of our choice from the page depending upon our need. Each programmer may name their textbox differently in their own project so I just wanted generic strings that can be called into our project as needed. The IP info is not sitting in limbo until we need it. The function gets called , the IP gets queried, then we have our info.
CementTruckAuthor Commented:
There was a lot more to this than I thought. We kind of have it working but are having issues with onblur/onclick/onchange that have nothing to do with the original question.

I have this in the aspx page
 <asp:Label ID="lblSubNet1" runat="server" Text="Label" Font-Bold="True" Visible="false"></asp:Label>
            <asp:TextBox ID="txtSubnet1a" Width="25px" runat="server" onblur="ResetGateway()"></asp:TextBox>.
            <asp:TextBox ID="txtSubnet1b" Width="25px" runat="server" onblur="ResetGateway()"></asp:TextBox>.
            <asp:TextBox ID="txtSubnet1c" Width="25px" runat="server" onblur="ResetGateway()"></asp:TextBox>.
            <asp:TextBox ID="txtIP1" Width="25px" runat="server"></asp:TextBox>

            <asp:TextBox ID="txtIP2" Width="25px" runat="server"></asp:TextBox>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LinkButton ID="btnAction" CssClass="LinkButton" runat="server"></asp:LinkButton>
<br />

Open in new window

and this further down for the javascript

    function ResetGateway() {

        var SubA = document.getElementById('<%= txtSubnet1a.ClientID %>');
        var SubB = document.getElementById('<%= txtSubnet1b.ClientID %>');
        var SubC = document.getElementById('<%= txtSubnet1c.ClientID %>');

        var actionbutton = document.getElementById('<%= btnAction.ClientID %>'); = "getSubNet("+SubA.innerHTML + '.' + SubB.innerHTML + '.' + SubC.innerHTML + '.1'+");";

Open in new window

Then I have this in the .vb page

        If e.CommandName = "PingAll" Then
            Dim AJ As New AjaxService

            pnlActions.Visible = True
            btnAction.OnClientClick = "getSubNet('" & e.CommandArgument & "'); return false;"
            lblSubNet1.Text = AJ.GetSubNet(e.CommandArgument)
            txtSubnet1a.text = AJ.GetIP(e.CommandArgument)(0)
            txtSubnet1b.text = AJ.GetIP(e.CommandArgument)(1)
            txtSubnet1c.text = AJ.GetIP(e.CommandArgument)(2)
            txtIP1.Text = "1"
            'lblSubNet2.Text = AJ.GetSubNet(e.CommandArgument)
            txtIP2.Text = "254"
            'btnAction.OnClientClick = "getSubNet('" & txtSubnet1a.text & "." & txtSubnet1b.text & "." & txtSubnet1c.text & "." & txtIP1.Text & "'); return false;"
            'btnAction.CommandArgument = e.CommandArgument
        End If

Open in new window

and lastly, we did this on the repository page. It is probably useless to have it there and could have been done on the actual asp page itself.

    Public Function GetIP(ByVal GateWay As String) As String()

        Dim sArr() As String = GateWay.Split(".")

        Return sArr

    End Function

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.