JS过滤器允许带AND而不带破折号的数字

时间:2017-08-15 03:19:40

标签: javascript filter html-table

我需要过滤一个在输入时过滤直播的表格。它将使用电话号码,但需要能够接受带有和不带短划线的号码。搜索该号码时,应该可以接受905-000-7777和9050007777。

这就是我到目前为止,我已经尝试了几个小时:

<!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 for names.." title="Type in a 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");
  for (i = 0; i < 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>

目前我可以让它过滤数字,但在电话号码中,您必须完美匹配表格变量。我希望它接受这个或那个。我对js的经验不多,而且事实证明这比我想象的要困难。

2 个答案:

答案 0 :(得分:0)

正如其他人已在评论中解释的那样,您可以在比较之前从输入和单元格内容中删除-个连字符。请参阅下面的工作小提琴:

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("-","");
  
  for (i = 0; i < 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 for names.." title="Type in a 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>

答案 1 :(得分:0)

你可以使用正则表达式,这将是这样的:

/^(\d+-?)+\d+$/