javascript - JS filter to allow numbers with AND without dashes -
i need filter table filters live while typing. use phone numbers, needs have ability accept numbers , without dashes. 905-000-7777 , 9050007777 should both acceptable when searching number.
this have far, i've been trying hours:
<!doctype html> <html> <head> <style> * { box-sizing: border-box; } #myinput { background-image: url('/css/searchicon.png'); background-position: 10px 10px; background-repeat: no-repeat; width: 100%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } #mytable { border-collapse: collapse; width: 100%; border: 1px solid #ddd; font-size: 18px; } #mytable th, #mytable td { text-align: left; padding: 12px; } #mytable tr { border-bottom: 1px solid #ddd; } #mytable tr.header, #mytable tr:hover { background-color: #f1f1f1; } </style> </head> <body> <h2>number search</h2> <input type="text" id="myinput" onkeyup="myfunction()" placeholder="search names.." title="type in name"> <table id="mytable"> <tr class="header"> <th style="width:60%;">number</th> </tr> <tr> <td>905-000-7777</td> </tr> <tr> <td>905-282-9992</td> </tr> <tr> <td>9050107777</td> </tr> </table> <script> function myfunction() { var input, filter, table, tr, td, i; input = document.getelementbyid("myinput"); filter = input.value.touppercase(); table = document.getelementbyid("mytable"); tr = table.getelementsbytagname("tr"); (i = 0; < tr.length; i++) { td = tr[i].getelementsbytagname("td")[0]; if (td) { if (td.innerhtml.touppercase().indexof(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } </script> </body> </html>
currently can have filter numbers in phone number, must match table variable perfectly. i'd accept or that. don't have experience js , proving more difficult thought.
as others have explained in comments, can remove -
hyphens both input , cell contents before comparing. see working fiddle below:
cleanedfilter = filter.replace("-",""); cellcontent = td.innerhtml.touppercase().replace("-","");
function myfunction() { var input, filter, table, tr, td, i, cleanedfilter; input = document.getelementbyid("myinput"); filter = input.value.touppercase(); table = document.getelementbyid("mytable"); tr = table.getelementsbytagname("tr"); cleanedfilter = filter.replace("-",""); (i = 0; < tr.length; i++) { td = tr[i].getelementsbytagname("td")[0]; if (td) { cellcontent = td.innerhtml.touppercase().replace("-",""); if (cellcontent.indexof(cleanedfilter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } }
* { box-sizing: border-box; } #myinput { background-image: url('/css/searchicon.png'); background-position: 10px 10px; background-repeat: no-repeat; width: 100%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } #mytable { border-collapse: collapse; width: 100%; border: 1px solid #ddd; font-size: 18px; } #mytable th, #mytable td { text-align: left; padding: 12px; } #mytable tr { border-bottom: 1px solid #ddd; } #mytable tr.header, #mytable tr:hover { background-color: #f1f1f1; }
<h2>number search</h2> <input type="text" id="myinput" onkeyup="myfunction()" placeholder="search names.." title="type in name"> <table id="mytable"> <tr class="header"> <th style="width:60%;">number</th> </tr> <tr> <td>905-000-7777</td> </tr> <tr> <td>905-282-9992</td> </tr> <tr> <td>9050107777</td> </tr> </table>
Comments
Post a Comment