...

View Full Version : get value of lines in textarea for duplicates check??



homerUK
03-04-2003, 04:00 PM
hey,

is there a way that I can count the number of lines witten in side a textarea tag, then compare these to make sure there are not any duplicate values??

I have the textarea tag which people write one sentance followed by a return key press...


eg values:

today
tomorrow
sometime soon
never
often
tomorrow

so I need some way of detecting that there is in fact a duplicate value in the above text. Is this possible??

Many thanks.....

beetle
03-04-2003, 04:56 PM
This seems to work
<html>
<head>
<title>TEST</title>

<script type="text/javascript">

function findDupes( ta )
{
var data = ta.value.split( /\r\n|\r|\n/ );
var found = new Array();

for ( var item, i = 0; ( item = data[i] ); i++ )
{
if ( !found.contains( item ) )
found[i] = item;
else
{
alert( "The value '" + item + "' is duplicated on line " + ( i + 1 ) );
return;
}
}
alert( "No duplicates found" );
}

Array.prototype.contains = function( val )
{
for ( var item, i = 0; ( item = this[i] ); i++ )
if ( item == val )
return true;
return false;
}

</script>

</head>

<body>

<form>
<textarea name="list" rows="10" cols="40"></textarea>
<input type="button" value="Check for dupes" onclick="findDupes( this.form.list )" />
</form>


</body>
</html>altough, this check explicit values. that means "word" is different from "word " (extra space). I'll wait for your reply on how you'd like to handle ambiguities such as extra spaces or capitilization differences.

homerUK
03-04-2003, 05:41 PM
hey man that works perfectly!!! Geeeeenious!! :thumbsup: :thumbsup:

I assume I can run a strip spaces and convert the strings to upper chars to compare them? that way they all should be identicle.....??

Also.... I want to try using the following command:

ok = findDupes( document.frm.options )

cos I have a function called "check" which is called when the form is submitted... at the moment I have a few checks, and have the following for your code:



if (ok) {
alert("ok");
return true;
} else {
alert("error");
return false;
}


how do I get your code to return a true if there are no dupes... and a false if there are??

Many thanks!! this is perfect!! :thumbsup: :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum