Javascript how can I find character “utf-16” at string

Clash Royale CLAN TAG#URR8PPPJavascript how can I find character “utf-16” at string
Have a question. Sorry for my English.
I have a string. Customer enter SMS text at text-area.
How can I find the existing character of utf-16 at the string or not?
utf-16
At php I check this code:
if (iconv("UTF-8","UTF-8//IGNORE",$_entry_text) != $_entry_text) {
// exist utf-16
}
How can I at Javascript check? Try to find an answer the second day ((
Thanks.
3 Answers
3
If it's a short string, one method would be to just look across the length of it and check if any of the char-codes sit outside of the single-byte 0-255 range
if (_entry_text.charCodeAt(i) > 255) ...
I should have elaborated, but UTF-8 uses both single and double bytes, UTF-16 would sit outside of the UTF-8 range, which is why it's > 255, and not >127
– Fool
3 mins ago
A string is a series of characters, each which have a character code. ASCII defines characters from 0 to 127, so if a character in the string has a code greater than that, then it is a Unicode character. This function checks for that. See String#charCodeAt.
function hasUnicode (str) {
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 127)
return true;
}
return false;
}
Then use it like, hasUnicode("Test message");
I think there is no need for a loop;
var ascii = /^[ -~]+$/;
ascii.test("Sefa"); // it is true no non-ascii characters
ascii.test("Sefa£"); // it is false there is non-ascii character
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I might be mistaken, or not understanding your question properly, but if they do enter it in a textarea, then the encoding should be set by the browser, and hence by your own page's encoding.
– Kaiido
1 hour ago