Once there was a search input.
It was responsible for filtering data in a table based on user input.
But this search input was special: it would not do anything unless a minimum of 3 characters was entered.
Not because it was lazy, but because it didn't make sense otherwise.

Everything was good until a new and strange (compared to English) language came to town.
It was Japanese and now the minimum string length of 3 was stupid and useless.

I lost the last few pages of that story. Does anyone remember how it ends?

有帮助吗?

解决方案 2

I guess it mainly depends on where you get that min length variable from. If it's hardcoded, you'd probably better use a dynamic internationalization module:

int.getMinStringLength(int.getCurrentLanguage())

Either you have a dynamic bindings framework such as AngularJS, or you update that module when the user changes the language.

Now maybe you'd want to sort your supported languages by using grouping attributes such as "verbose" and "condensed".

其他提示

In order to fix the issue, you obviously need to determine if user's input belongs to certain script(s). The most obvious way to do this is to use Unicode Regular Expressions:

var regexPattern = "[\\p{Katakana}\\p{Hiragana}\\p{Han}]+";

The only issue would be, that JavaScript does not support this kind of regular expressions out of the box. Anyway, you are lucky - there is a JS library called XRegExp and its Scripts add-on seems to exactly what you need. Now, the question is, whether you want to require at least three characters for non-Japanese or non-Chinese users, or do it otherwise - require at least three characters for certain scripts (Latin, Common, Cyrillic, Greek and Hebrew) while allowing any other to be searched on one character. I'd suggest the second solution:

if (XRegExp('[\\p{Latin}\\p{Common}\\p{Cyrillic}\\p{Greek}\\p{Hebrew}]+').test(input)) {
    // test for string length and call AJAX if the string is long enough
} else {
    // call AJAX search method
}

You might want to pre-compile the regular expression for better performance, but that's basically it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top