I'm using Typeahead jQuery library, but when a user types a vocal like e
it should match foreign vocals too, like é
, ë
, è
.
Match n
with ñ
too.
This is my code:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="typeahead.css">
<script src="jquery-1.11.0.min.js"></script>
<script src="typeahead.bundle.min.js"></script>
</head>
<body>
<center>
<div id="nombre">
<input class="typeahead" type="text" placeholder="Nombre">
</div>
</center>
<script>
var charMap = {
"à": "a",
"á": "a",
"ä": "a",
"è": "e",
"é": "e",
"ë": "e",
"ì": "i",
"í": "i",
"ï": "i",
"ò": "o",
"ó": "o",
"ö": "o",
"ù": "u",
"ú": "u",
"ü": "u",
"ñ": "n"};
var normalize = function (input) {
$.each(charMap, function (unnormalizedChar, normalizedChar) {
var regex = new RegExp(unnormalizedChar, 'gi');
input = input.replace(regex, normalizedChar);
});
return input;
}
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
matches = [];
substrRegex = new RegExp(q, "i");
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push({ value: str });
}
});
cb(matches);
};
};
var nombres = ["Sánchez", "Árbol", "Müller", "Ératio", "Niño"];
$("#nombre .typeahead").typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: "nombres",
displayKey: "value",
source: substringMatcher(nombres)
});
</script>
</body>
How can I achieve this?
Thanks!