Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    get value of lines in textarea for duplicates check??

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

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This seems to work
    Code:
    <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.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    hey man that works perfectly!!! Geeeeenious!!

    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:

    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!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •