Reputation: 11
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?
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.
Upvotes: 0
Views: 1881
Reputation: 504
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
Upvotes: 0
Reputation: 3329
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");
Upvotes: 3
Reputation: 119
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) ...
Upvotes: 0