javascript - Detecting if inputs match by having common elements -
i have 2 input boxes , trying compare them, in order see if have common elements. reason, though, not working. code:
html:
personal address line 1: <input type = "text" id = "persadd1" /> address line 1: <input type = "text" id = "addline1" /> <input type = "submit" value = "continue" onclick = "vali()" />
javascript:
function vali() { var add1str = document.getelementbyid('persadd1').value; var add2str = document.getelementbyid('addline1').value; var arr1 = add1str.split(" "); var arr2 = add2str.split(" "); var arr3 = []; (var = 0; < arr1.length; i++) { (var j = 0; j < arr2.length; j++) { if (arr1[i] === arr2[j]) { arr3.push( arr1[i] ); if(arr3 !== ""){ alert("error"); } } } } }
any ideas?
the below work you.
function vali() { var add1str = document.getelementbyid('persadd1').value; var add2str = document.getelementbyid('addline1').value; var arr1 = add1str.split(" "); var arr2 = add2str.split(" "); var arr3 = []; (var = 0; < arr1.length; i++) { (var j = 0; j < arr2.length; j++) { if (arr1[i] === arr2[j]) { arr3.push(arr1[i]); //console.log(arr3); } } } if (arr3.length > 0) { //check if array 3 has items, if yes alert error. alert("error"); } }
Comments
Post a Comment