0

i have something like this

<input name="ip" id="ip">

this ip needs to be added to a list of ips that contains multiple ips in this format

<select id="iplist" name="iplist">
<option value="192.168.1.2">192.168.1.2</option>
<option value="192.168.1.10 - admin">192.168.1.10 - admin</option>
<option value="192.168.2.2 - test">192.168.2.2 - test</option>
<option value="192.168.2.9 - test1">192.168.2.9 - test1</option>
</select>

and many other values in the select list (not only the ones listed), when the button is executed and the ip needs to be added to the select option I need to check if the ip is already in the list or not, if is not to return error,

so i have something like this to get the values

var iplist = $.map($('#iplist option'), function(e) { return e.value; });

and console.log is showing all the values in array

console.log(iplist);

To get the values as string i can use

var str = iplist.join(',');

However I have a very hard time to verify if the ip is already in the select list.

Any help is really appreciated, thank you in advance for your time taken to look into this. I'm thinking of regex to verify if the ip exist in array, but I have problems with that because there are multiple values types, delimited by comma also with space, and also without space

sasha
  • 13
  • 4
  • You can pick one of the answers from this question - https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array. – Tsvetan Ganev Dec 25 '17 at 22:41
  • is not a duplicate, is something different. because the value that needs to be verified from array is in different format then the value from the input field. – sasha Dec 25 '17 at 23:21
  • Not quite duplicate, I agree. In this case `1.2.3.4 abc` and `.1.2.3.4 def` are equal. That's additional logic worthy of a new answer. – Rudie Dec 26 '17 at 00:11
  • I just searched on meta, and first of all, asking two questions in one is frowned upon, and that is really what we have here. Even broken down into two questions, both are duplicates, the other duplicate being https://stackoverflow.com/q/10272773/34806 for extracting the IP address. Finally, this probably could have been closed as unclear, since the OP doesn't even tell us that the above is part of what he needs. – Dexygen Dec 26 '17 at 10:53
  • Is not a duplicate, look at the @orabis answer and you will understand – sasha May 04 '18 at 23:14

1 Answers1

0

With the assumption that there is a button, which is not in the html you showed, you can use the code below. The regular expression used is from another Stackoverflow post (see Extract ip address from a string using regex )

   var r = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
var inputvalue = document.getElementById("ip").value;
inputvalue  = inputvalue.match(r);
if(!inputvalue){
    return console.log("bad input");
}

inputvalue = inputvalue[0];

if($("select#iplist option").filter(function(){return $(this).val().match(r)[0] === inputvalue}).length){
    console.log("exists");
}else{
    $('select#iplist').append($('<option>', {value: inputvalue, text: inputvalue}));
}

function myFunction(){

var r = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
var inputvalue = document.getElementById("ip").value;
inputvalue  = inputvalue.match(r);
if(!inputvalue){
 return console.log("bad input");
}

inputvalue = inputvalue[0];

if($("select#iplist option").filter(function(){return $(this).val().match(r)[0] === inputvalue}).length){
 console.log("exists");
}else{
 $('select#iplist').append($('<option>', {value: inputvalue, text: inputvalue}));
}

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

    <select id="iplist" name="iplist">
<option value="192.168.1.2">192.168.1.2</option>
<option value="192.168.1.10 - admin">192.168.1.10 - admin</option>
<option value="192.168.2.2 - test">192.168.2.2 - test</option>
<option value="192.168.2.9 - test1">192.168.2.9 - test1</option>
</select>

<input name="ip" id="ip"/>
<button onclick="myFunction()">add</button>
</body>
orabis
  • 2,749
  • 2
  • 13
  • 29
  • Indeed i know about this, the problem is that for example if you check the select value that is "192.168.1.10 - admin" and you verify if ip 192.168.1.10 exist it will not work because the value is different then 192.168.1.10 - admin – sasha Dec 25 '17 at 23:06
  • My apology. What are the other patterns you need to check? Because if you are testing against an IP that is followed by some string; admin, etc, then you can only check if the value starts with this number. Otherwise, we need to write a regular expression. But first, I need to know the patterns you are considering – orabis Dec 25 '17 at 23:10
  • thank you for your fast reply, indeed its starts with a number, it can be like this "192.168.1.2" or "192.168.1.3 - admin" only this 2 types of formating . – sasha Dec 25 '17 at 23:11
  • But i only want to check if the ip is in the select option value. on the input field only an ip is added, so the comparison needs to be made only for the first part of the value. – sasha Dec 25 '17 at 23:19
  • I updated the code to use a regular expression to extract the ip. Then, we compare the IPs extracted. In such a case, we will be able to assume values such as 192.0.0.1 and 192.0.0.1 - Admin the same. – orabis Dec 25 '17 at 23:24
  • How did it go?. – orabis Dec 26 '17 at 00:55
  • 1
    is working very good, thank you very much for your help, the regex helped a lot. Thank you again – sasha May 04 '18 at 23:13