guys i have a problem with this thing , i must know which language the user is using while typing in an input text to handle some bad shaped things and i even looked at this in here but it didn't helped me , can u guys help me with the right code ?
the languages i must know are english and persian :| thanks :)
i need some javascript helps
<input type="text" id="give_me_the_lang">
<div id="show_lang_in_here"></div>
Google Translate - If you need to determine the language of an entire web page or an online document, paste the URL of that page in the Google Translate box and choose “Detect Language” as the source language.
On the Review tab, in the Language group, click Language. Click Set Proofing Language. In the Language dialog box, select the Detect language automatically check box. Review the languages shown above the double line in the Mark selected text as list.
Here is the solution that doesn't really detect language, but just checks if users input contains only English alphabet. If it does, it writes "English" in div, otherwise it writes "Persian". Code:
document.getElementById("give_me_the_lang").addEventListener("keyup", function() {
if (/^[a-zA-Z]+$/.test(this.value)) {
document.getElementById("show_lang_in_here").innerHTML = "English";
} else {
document.getElementById("show_lang_in_here").innerHTML = "Persian";
}
});
<input type="text" id="give_me_the_lang">
<div id="show_lang_in_here"></div>
Expression ^[a-zA-Z]+$
only checks the letters of English alphabet, if you write in number or any other character, the result will be "Persian".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With