Skip to content Skip to sidebar Skip to footer

Javascript Doesn't Recognize Turkish Characters

I have this code : if (term.length > 0) { var inputVal = $('#input').val(); if (inputVal.indexOf('jason') !== -1) { results = $.ui.autocomplete.filter(table, term); }

Solution 1:

It does recognize Turkish characters, but you are doing an equality check on said string. What you have to understand is that even if ö is only an o with an accent, it's still a different character, thus making your conditional falsy, even if you are not doing a triple equality check.

'ö' == 'o'  // false'ö' === 'o' // false

What you should do instead is convert the input value into a string without accents, since it's apparently what you were expecting. And this question is exactly what you are looking for, and I would say this answer would be the best one if you have access to ES6 features since it's pretty clean and simple to use

functionconvert (str) {
  return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}

if (term.length > 0) {
  var inputVal = convert($('#input').val())
  if (inputVal.indexOf('jason') !== -1) {
    results = $.ui.autocomplete.filter(table, term)
  }
}

Otherwise if ES6 is not an option, the top answer should be fine to use, just create a function util that you can reuse anywhere you need to.

Regarding the encoding of the file, you should use utf-8 if dealing with special chars that Turkish does have.

Post a Comment for "Javascript Doesn't Recognize Turkish Characters"