2

I have a javascript which appends a string like 222222222222222 to another field (which will either be blank or already have numbers like 222222222222222 33333333333333) with a click of a button. Actually it's 15 digit IMEI of the phone. User has the option of submitting a single IMEI or bulk IMEI. When more then one IMEI is added to the bulk field by pressing the button from myVar1, the new IMEI gets inserted below the previous IMEI in the bulk field(myVar2).

Currently, I am using the below script to do this and it's working perfectly fine. The problem is that it doesn't check for duplicates before appending.

function append_myVar1_to_myVar2(){
var myVar1 = document.getElementById('myVar1_value').value;
var myVar2 = document.getElementById('myVar2_value').value;

    if(document.getElementById('myVar2_value').value == ''){
        document.getElementById('myVar2_value').value = myVar1; 
    }else{
        document.getElementById('myVar2_value').value = document.getElementById('myVar2_value').value + "\r\n" + myVar1; 
    }
}

I have modified the script now as below (updated to include the first response, thanks to Brian) to check for duplicates, but it's not working. Request experts to have a look into it.

function append_myVar1_to_myVar2(){
 var myVar1 = document.getElementById('myVar1_value').value;
 var myVar2 = document.getElementById('myVar2_value').value;

    if(document.getElementById('myVar2_value').value == ''){
        document.getElementById('myVar2_value').value = myVar1; 
    }else{
        var flag = 0;
                    var wordsarr = myVar2.split("\r\n");
        for(var i = 0; i < wordsarr.length; i++)
            {
        if(wordsarr[i].value == myVar1)
        {   
        flag = 1;
        }
            }
        if(flag == 1)
        {
        alert('Value is duplicate.');
        }
    else{
        document.getElementById('myVar2_value').value = document.getElementById('myVar2_value').value + "\r\n" + myVar1; 
    }
}}

Here is the html of the page:

<html>
<body>

<input id="myVar1_value" type="text" maxlength="15" name="myVar1_value">
<input id="IMEI_ADD" class="button_gray" type="button"     onclick="append_myVar1_to_myVar2()" value="Add this IMEI to bulk entry" name="IMEI_ADD">

<p id="imei_bulk_field" class="form-row notes">
<textarea id="myVar2_value" class="input-text" rows="2" cols="5" placeholder="If you have more than one IMEI, insert them here by pressing the button above." name="myVar2_value"></textarea>
</p>

</body>
</html>         
4
  • 3
    What do you mean by "not working"? What does it do and what do you want it to do? Commented Jun 19, 2013 at 12:29
  • it would be helpful to see html of what we are talking about here Commented Jun 19, 2013 at 12:30
  • Thanks Aleks/mcgrailm for quickly reverting. "Not working" means I can still add a duplicate value to the second field by clicking the button and don't get any alert. Whereas I am supposed to get an alert. I'll modify the question to include relevant html. Commented Jun 19, 2013 at 13:01
  • Please, create jsfiddle , that we could work with. Commented Jun 19, 2013 at 13:06

4 Answers 4

2
        for(var i = 0; i < (myVar2.split("\r\n")).length; i++)
        {
        //here is wrong
            if(myVar2[i].value == myVar1)
            {   
                flag = 1;
            }

You should change to

       var wordsarr = myVar2.split("\n");
       for(var i = 0; i < worsarr.length; i++)
       {
          if(wordsarr[i] == myVar1)
          {   
             flag = 1;
          }
       }
       if(flag == 1)
       {
          alert('Value is duplicate.');
       }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Brian for correcting that part of code. Obviously I was messing up there. By seeing the code it looks like it should work perfectly, but not sure why it's still not helping. Could this be browser issue. I tested it on the latest version of Chrome and Firefox without any luck.
I think the split word '\r\n' is the key.
Yes, only 1st value is being considered in the duplicate check, which means there is a problem in the split itself such that the count always is taken as one no matter how many values are there. Out for some work now, will check few more combination as soon I am back.
Awesome!!!! Strange, but changing myVar2.split("\r\n") to myVar2.split("\n") did the trick. Just tried that and came here to post and noticed that you have also suggested the same. Your suggestion is absolutely correct.... all 5 stars :)
2

Store splitted chunks ,and iterate over them:

var chunkArray = myVar2.split("\r\n");

for(var i = 0; i !== chunkArray.length; i++){
    if(chunkArray[i] == myVar1){   
        flag = 1;
        break;
    }
}

1 Comment

Thanks Engineer, if the previous solution works, even this would work. But probably there is something that I am missing. I'll edit the question to include more details.
1
var myVar2 = document.getElementById('myVar2_value').value;

Later...

if(myVar2[i].value == myVar1)

It looks like you are adding .value when you don't need to. Try:

if(myVar2[i] == myVar1)

1 Comment

Yes, removing ".value" worked. Now I get the alert when adding a duplicate value immediately. Thanks a lot for the tips. Still one problem though, I add one value, then I add another different value and then I try to add the first value again, this time I don't get an alert. I am getting an alert only when I try to add an immediate duplicate value.
0

This could be of assistance

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

you could change the if with:

haystack[i].value == needle

Comments

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.