Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Possible to sort and omit duplicates in a list using javascript

Reply
Thread Tools

Possible to sort and omit duplicates in a list using javascript

 
 
Laphan
Guest
Posts: n/a
 
      02-03-2008
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



 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      02-05-2008

"Laphan" <(E-Mail Removed)> wrote in message
news:uK%(E-Mail Removed)...
> 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[i], 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


 
Reply With Quote
 
 
 
 
Laphan
Guest
Posts: n/a
 
      02-12-2008
Hi Guys

Can't thank you enough for this.

It is very much appreciated.

Rgds

"Anthony Jones" <(E-Mail Removed)> wrote in message
news:OGbW%23E%(E-Mail Removed)...

"Laphan" <(E-Mail Removed)> wrote in message
news:uK%(E-Mail Removed)...
> 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[i], 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort and remove duplicates JimJx Perl Misc 5 09-28-2007 08:54 PM
Using =~ to Include and Omit in same line Walt Perl Misc 18 04-29-2006 05:19 PM
QUESTION: Good data structure for list within list with duplicates? basi Ruby 4 08-01-2005 07:30 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
Please illustrate how to omit null field from a DB using a datalist. Troy ASP .Net 1 08-26-2003 07:03 PM



Advertisments