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

03-04-2003, 04:00 PM

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:

sometime soon

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

Many thanks.....

03-04-2003, 04:56 PM
This seems to work

<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;
alert( "The value '" + item + "' is duplicated on line " + ( i + 1 ) );
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;




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

</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.

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) {
return true;
} else {
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: