Let's say your oninput
function looks like this (a very simple example):
function onInputHandler(value) {
console.log(value);
}
Now, I found this answer, and I modified the function slightly to deal with sentences/multiple letters:
function letter(phrase) {
var output = false;
var chars = phrase.split("");
chars.forEach(c => {if (!c.toUpperCase() != c.toLowerCase()) output = true});
return output;
}
And we can implement that into our oninput
handler like so:
function onInputHander(value) {
if (!letter(value) {
console.log(value);
}
}
So it will print out the value as long as it is not a letter. Here's a demonstration:
function letter(phrase) {
var output = false;
var chars = phrase.split("");
chars.forEach(c => {if (!c.toUpperCase() != c.toLowerCase()) output = true});
return output;
}
function onInputHandler(value) {
if (!letter(value)) {
console.log(value);
}
}
<input type="text" oninput="onInputHandler(this.value)">
But you'll notice in the above snippet, it also prints out whitespace - try it! I added another condition to the if
statement in onInputHandler
:
if (!isLetter(value) && value.trim() != "") {
console.log(value);
}
Now this will not allow any characters which have differing uppercase/lowercase values (most alphabets, including Latin (English), Greek, and Cyrillic (Russian).
However, if you want to do it another way, and check if each character in the string is a Chinese character, you could look at this question and its answers. Here's an example of a regular expression you could construct for Chinese characters (not perfect, but it covers most of the characters):
const chineseCharacterRegex = /[\u4e00-\u9fff]|[\u3400-\u4dbf]|[\u{20000}-\u{2a6df}]|[\u{2a700}-\u{2b73f}]|[\u{2b740}-\u{2b81f}]|[\u{2b820}-\u{2ceaf}]|[\uf900-\ufaff]|[\u3300-\u33ff]|[\ufe30-\ufe4f]|[\uf900-\ufaff]|[\u{2f800}-\u{2fa1f}]/u;
And here's a function that checks if all characters in a string match that regex:
function chineseChar(phrase) {
var output = true;
var chars = phrase.split("");
chars.forEach(c => {if (!c.match(chineseCharacterRegex)) output = false);
return output;
}
Now we can implement this in our function like so:
const chineseCharacterRegex = /[\u4e00-\u9fff]|[\u3400-\u4dbf]|[\u{20000}-\u{2a6df}]|[\u{2a700}-\u{2b73f}]|[\u{2b740}-\u{2b81f}]|[\u{2b820}-\u{2ceaf}]|[\uf900-\ufaff]|[\u3300-\u33ff]|[\ufe30-\ufe4f]|[\uf900-\ufaff]|[\u{2f800}-\u{2fa1f}]/u;
function onInputHandler(value) {
if (chineseChar(value)) {
console.log(value);
}
}
Note: I do know the regex doesn't match all the Chinese characters, but this answer tells you why, and you can find most of the other character unicodes by reading the answers and following the links in this thread.
Hopefully this helps!