javascript - Sanitize searchable string JS -
trying make possible searching string character, should removed before shooting find function.
e.g. search string "john", while want returned possible variations "^" within (j^ohn, jo^hn, joh^n, ^john, john^) correct ones.
js part:
$(document).ready(function() { $('#search').keyup(function() { searchtable($(this).val()); }); }); function searchtable(inputval) { var table = $('#tbldata'); table.find('tr').each(function(index, row) { var allcells = $(row).find('td'); if(allcells.length > 0) { var found = false; allcells.each(function(index, td) { var regexp = new regexp(inputval, 'i'); if(regexp.test($(td).text())) { found = true; return false; } }); if(found == true)$(row).show();else $(row).hide(); } }); }
html part:
<div class="tables"> <p> <label for="search"> <strong>enter name</strong> </label> <input type="text" id="search"/> <label></label> </p> <table id="tbldata" class="target table table-bordered table-striped"> <tr> <th>first name</th> <th>last name</th> <th>number</th> <th>letters</th> <th>gender</th> <th>company</th> </tr> <tr> <td>jo^hn</td> <td>lenno^n</td> <td>151</td> <td>sf</td> <td>male</td> <td>sms global (technologies)</td> </tr> <tr> <td>pau^l</td> <td>mcc^artney</td> <td>753</td> <td>tj</td> <td>male</td> <td>sms global (services)</td> </tr> <tr> <td>george</td> <td>harrison</td> <td>24</td> <td>ge</td> <td>female</td> <td>sms aim technologies</td> </tr> <tr> <td>ringo</td> <td>starr</td> <td>26</td> <td>hg</td> <td>female</td> <td>sms global (help desk)</td> </tr> </table>
here example:
simply can replace "^" , try compare input string data below.
var inputstring="john"; inputstring=inputstring.tolowercase(); var text="joh^n"; text=text.tolowercase(); text=text.replace("^",""); (text===inputstring)?true:false;
if want use text similarity can use text similarity algorithms such levenshtein string matching algorithm, quick solution try library: fuzzyset
Comments
Post a Comment