Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate check in javascript before inserting a new record
    primarykey
    data
    text
    <p>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).</p> <p>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.</p> <pre><code>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; } } </code></pre> <p>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.</p> <pre><code>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 &lt; 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; } }} </code></pre> <p>Here is the html of the page:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;input id="myVar1_value" type="text" maxlength="15" name="myVar1_value"&gt; &lt;input id="IMEI_ADD" class="button_gray" type="button" onclick="append_myVar1_to_myVar2()" value="Add this IMEI to bulk entry" name="IMEI_ADD"&gt; &lt;p id="imei_bulk_field" class="form-row notes"&gt; &lt;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"&gt;&lt;/textarea&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload