1

I have 'n' numbers in a TEXTAREA on a form. After the user enters some value in the TEXTAREA, I need to validate it's not a duplicate of any other value.

Example:

TEXTAREA_1 value =
10
20
30
40
10

It should alert and say 'value 10 was entered more than once'. Please help me with the JavaScript.

3 Answers 3

1
var nums = textarea.value.split('\n').sort();
var prev;
for (var i = 0; i < nums.length; i++ ) {
    if (prev && nums[i] == prev) {
        alert('value ' + prev + ' was entered more than once');
        break;
    }
    prev = nums[i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hai Anatoliy, your answer is right for me, now my script for this scipt has complete. thank you very-very much...
+1 leveraging sort. However note this is purely string-based, it doesn't do numerical comparison so '0' is a different thing to '00' or '0.0'.
0
$(function() {
  $("form").submit(function() {
    var txt = $("#TEXTAREA_1");
    var vals = txt.val().split("\n");
    var len = vals.length;

    for(var i = 0; i < len; i++) {
      for(var j = i + 1; j < len; j++) {
        if(vals[i] === vals[j]) {
          alert("Duplicate values are not allowed");
          txt.select();
          return false;
        }
      }
    }
  });
});

It works

2 Comments

Hai Josh, thank you for your answering, i have the answer from Anatoly. thank you very much...
You're welcome! I've actually had to do this before. This could be easily changed to not use jQuery, too.
0

pure DOM/JS solution:

  <form id="foo">
    <textarea id="numbers-textfield" cols=40 rows=20>10
    20
    30
    40
    50
    60
    60</textarea>
    <br>
    <input type=submit value=submit>
</form>
<script>
    (function() {
    var form = document.getElementById('foo'),
        textarea = document.getElementById('numbers-textfield'),
        inArray = function( value, arr ) {
        for ( var i = arr.length; i--; ) {
            if ( value == arr[i] ) {
            return true;
            }
        }
        return false;
        }


    form.onsubmit = function() {
        var value = textarea.value.split(/\s+/), len = value.length, stack = [], errors = false;


        for ( var i = 0; i<len; ++i ) {
        var isNum = !isNaN( value[i] );
        if ( !isNum ) { continue; }
        if ( errors ) { break; }
        if ( !inArray( value[i], stack ) ) {
            stack.push( value[i] );
        } else {
            errors = true;
            alert( value[i] + ' already in stack');
        }
        }

        if ( errors ) {
        return false;
        }

    }

    })();
</script>

2 Comments

oh, I just misunderstood the question. Give me a min then.
hai Meder, thank for your answering, i have the answer from Anatoly. thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.