Possible to sort and omit duplicates in a list using javascript

Discussion in 'ASP General' started by Laphan, Feb 3, 2008.

  1. Laphan

    Laphan Guest

    Hi All

    I need to provide a function on my site whereby a user can enter a number of
    ip addresses (one per line) so that I can submit this en masse to string
    holding space in my db.

    My problem is that I want to keep this list sorted as they enter the ip
    addresses and I want to check for duplicates so that they don't enter the
    same ip addresses twice.

    I was going to do this as a simple form submit and add each entry as a
    database row so that I could ORDER BY to get the sort and check for
    duplicates on the submit, but this client/server submit really slows the
    whole process. It would be great if I could order by and duplicate check on
    the client side so that once done all I'm doing is one client/server 'post'
    to the db.

    Is this feasible?

    Thanks
    Laphan, Feb 3, 2008
    #1
    1. Advertising

  2. "Laphan" <> wrote in message
    news:uK%...
    > Hi All
    >
    > I need to provide a function on my site whereby a user can enter a number

    of
    > ip addresses (one per line) so that I can submit this en masse to string
    > holding space in my db.
    >
    > My problem is that I want to keep this list sorted as they enter the ip
    > addresses and I want to check for duplicates so that they don't enter the
    > same ip addresses twice.
    >
    > I was going to do this as a simple form submit and add each entry as a
    > database row so that I could ORDER BY to get the sort and check for
    > duplicates on the submit, but this client/server submit really slows the
    > whole process. It would be great if I could order by and duplicate check

    on
    > the client side so that once done all I'm doing is one client/server

    'post'
    > to the db.
    >



    Take a gander at this:-

    <html>
    <head>
    <title>Enter IPs</title>
    <script type="text/javascript">

    var masIPs = {}

    function cmdAdd_onclick()
    {
    var rgxIP =
    /^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4
    ]\d)|(25[0-5]))$/
    var sIP = document.getElementById("txtIP").value
    if (rgxIP.test(sIP))
    {

    masIPs[sIP] = true

    var arr = []
    for (var sIP in masIPs) arr.push(sIP)
    arr.sort()
    document.getElementById("txtIPs").value = arr.join("\r\n")
    document.getElementById("txtIP").value = ""
    }
    }
    </script>
    </head>
    <body>
    <form action="test.asp" method="post">
    <label>IP Address: </label>
    <input id="txtIP" type="text" value="" />
    <input type="button" value="Add" onclick="cmdAdd_onclick.call(this)"
    /><br />
    <textarea id="txtIPs" name="txtIPs">
    </textarea><br />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

    It uses a Javascript object as an associative array which ensures that the
    IPs are unique.

    However you might need to consider the sorting. Is alphanumeric sorting of
    IP addresses expected. e.g would you expect 20.67.39.4 to appear in the
    list after 192.168.34.2 ?

    If not then you need to enhance the sorting. Add this function to the
    Javascript:-

    function compareIPs(vsIP1, vsIP2)
    {
    var asIP1 = vsIP1.split(".")
    var asIP2 = vsIP2.split(".")

    var i = 0
    var retVal = 0
    while (!retVal && i<4)
    retVal = fnCompareOctet(asIP1, asIP2[i++])

    return retVal

    function fnCompareOctet(vs1, vs2)
    {
    var l1 = parseInt(vs1)
    var l2 = parseInt(vs2)
    return l1 < l2 ? -1 : l1 > l2 ? 1 : 0
    }

    }

    now change the array sort line to:-

    arr.sort(compareIPs)

    This makes use of javascript arrays sort method accepting a comparator
    function.

    How about being able to delete an IP address from the list and should the
    textarea be editable? I'll leave that as an exercise ;)


    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Feb 5, 2008
    #2
    1. Advertising

  3. Laphan

    Laphan Guest

    Hi Guys

    Can't thank you enough for this.

    It is very much appreciated.

    Rgds

    "Anthony Jones" <> wrote in message
    news:OGbW%23E%...

    "Laphan" <> wrote in message
    news:uK%...
    > Hi All
    >
    > I need to provide a function on my site whereby a user can enter a number

    of
    > ip addresses (one per line) so that I can submit this en masse to string
    > holding space in my db.
    >
    > My problem is that I want to keep this list sorted as they enter the ip
    > addresses and I want to check for duplicates so that they don't enter the
    > same ip addresses twice.
    >
    > I was going to do this as a simple form submit and add each entry as a
    > database row so that I could ORDER BY to get the sort and check for
    > duplicates on the submit, but this client/server submit really slows the
    > whole process. It would be great if I could order by and duplicate check

    on
    > the client side so that once done all I'm doing is one client/server

    'post'
    > to the db.
    >



    Take a gander at this:-

    <html>
    <head>
    <title>Enter IPs</title>
    <script type="text/javascript">

    var masIPs = {}

    function cmdAdd_onclick()
    {
    var rgxIP =
    /^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4
    ]\d)|(25[0-5]))$/
    var sIP = document.getElementById("txtIP").value
    if (rgxIP.test(sIP))
    {

    masIPs[sIP] = true

    var arr = []
    for (var sIP in masIPs) arr.push(sIP)
    arr.sort()
    document.getElementById("txtIPs").value = arr.join("\r\n")
    document.getElementById("txtIP").value = ""
    }
    }
    </script>
    </head>
    <body>
    <form action="test.asp" method="post">
    <label>IP Address: </label>
    <input id="txtIP" type="text" value="" />
    <input type="button" value="Add" onclick="cmdAdd_onclick.call(this)"
    /><br />
    <textarea id="txtIPs" name="txtIPs">
    </textarea><br />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

    It uses a Javascript object as an associative array which ensures that the
    IPs are unique.

    However you might need to consider the sorting. Is alphanumeric sorting of
    IP addresses expected. e.g would you expect 20.67.39.4 to appear in the
    list after 192.168.34.2 ?

    If not then you need to enhance the sorting. Add this function to the
    Javascript:-

    function compareIPs(vsIP1, vsIP2)
    {
    var asIP1 = vsIP1.split(".")
    var asIP2 = vsIP2.split(".")

    var i = 0
    var retVal = 0
    while (!retVal && i<4)
    retVal = fnCompareOctet(asIP1, asIP2[i++])

    return retVal

    function fnCompareOctet(vs1, vs2)
    {
    var l1 = parseInt(vs1)
    var l2 = parseInt(vs2)
    return l1 < l2 ? -1 : l1 > l2 ? 1 : 0
    }

    }

    now change the array sort line to:-

    arr.sort(compareIPs)

    This makes use of javascript arrays sort method accepting a comparator
    function.

    How about being able to delete an IP address from the list and should the
    textarea be editable? I'll leave that as an exercise ;)


    --
    Anthony Jones - MVP ASP/ASP.NET
    Laphan, Feb 12, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Troy
    Replies:
    1
    Views:
    351
    S. Justin Gengo
    Aug 26, 2003
  2. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
  3. basi
    Replies:
    4
    Views:
    168
    Wayne Vucenic
    Aug 1, 2005
  4. Walt
    Replies:
    18
    Views:
    159
    Dr.Ruud
    Apr 29, 2006
  5. JimJx

    Sort and remove duplicates

    JimJx, Sep 28, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    131
    Martijn Lievaart
    Sep 28, 2007
Loading...

Share This Page