ActionScriptで文字判定
テキストフィールドに入力された文字が全角か?半角か?などを調べる ActionScript です。
チェックする文字の文字コードを調べ、指定した範囲にその文字が存在するかなどを調べます。// コードチェック(文字,範囲小,範囲大)
function checkCode(str, min, max) {
var len = str.length;
while (len--) {
var num = str.substr(len, 1).charCodeAt(0);
if (nummax) {
return false;
break;
}
}
return true;
}
文字列が全て半角数字か、アルファベットの大文字/小文字、半角カタカナなどをチェックしたい場合は、文字と範囲を指定して以下のようになります。
// 半角数字か?
trace(checkCode("123", 0x30, 0x39)); //true
trace(checkCode("abc", 0x30, 0x39)); //false
// 半角大文字アルファベットか?
trace(checkCode("ABC", 0x41, 0x5a)); //true
// 半角小文字アルファベットか?
trace(checkCode("abc", 0x61, 0x7a)); //true
// 半角カタカナか?
trace(checkCode("abc", 0xFF61,0xFF9F)); //false
文字列が全て半角文字か判定、文字列のバイト数を調べる応用例です。// 文字列が全て半角文字かを判定
function checkHalf(str){
return (checkCode(str, 0x00, 0x7f) || checkCode(str, 0xFF61,0xFF9F)) ? true : false;
}
trace(checkHalf("AAA")); //true
trace(checkHalf("AあA")); //false
// 文字列のバイト数を調べる
function getStrBytesTotal(str) {
var cnt = 0;
var len = str.length;
while (len--) {
(checkHalf(str.substr(len, 1))) ? cnt++ : cnt += 2;
}
return cnt;
}
trace(getStrBytesTotal("ああああ")); //8
全角→半角変換を作ろうと思ったら、JavaScriptで公開してくれている人がいました。
AOK - 全角英数字・記号を半角に置換
半角カナの変換は1文字ずつ置換した方がよさそう...。
JavaScript例文辞典 - 半角カナから全角カナへ変換する
JavaScript例文辞典 - 半角カナから全角カナへ変換する(濁点等対応版)
IT用語辞典 - ASCII文字コード
Flash-ML - [flash:25713] Re:半角と全角の判定
Flash-ML - [flash:25698] タグを消去して単純なテキスト文章にする方法
GAC - Flash[6537] 半角
とほほ - 漢字コードについて
<追記>
2006/04/29 -AS2.0版は Flash OOP の 自作クラス公開・共有環境プロジェクト で公開しています。※プロジェクトは自然消滅しました(?)。
2005/04/07 - 「文字コード判定」という言い方は「 Shift-JIS か UTF-8 かを判断するスクリプト(?)」と思われるので、文章を少し改訂。
2005/03/17 - 全角半角変換等のリンク追加
チェックする文字の文字コードを調べ、指定した範囲にその文字が存在するかなどを調べます。
function checkCode(str, min, max) {
var len = str.length;
while (len--) {
var num = str.substr(len, 1).charCodeAt(0);
if (num
return false;
break;
}
}
return true;
}
文字列が全て半角数字か、アルファベットの大文字/小文字、半角カタカナなどをチェックしたい場合は、文字と範囲を指定して以下のようになります。
trace(checkCode("123", 0x30, 0x39)); //true
trace(checkCode("abc", 0x30, 0x39)); //false
// 半角大文字アルファベットか?
trace(checkCode("ABC", 0x41, 0x5a)); //true
// 半角小文字アルファベットか?
trace(checkCode("abc", 0x61, 0x7a)); //true
// 半角カタカナか?
trace(checkCode("abc", 0xFF61,0xFF9F)); //false
文字列が全て半角文字か判定、文字列のバイト数を調べる応用例です。
function checkHalf(str){
return (checkCode(str, 0x00, 0x7f) || checkCode(str, 0xFF61,0xFF9F)) ? true : false;
}
trace(checkHalf("AAA")); //true
trace(checkHalf("AあA")); //false
// 文字列のバイト数を調べる
function getStrBytesTotal(str) {
var cnt = 0;
var len = str.length;
while (len--) {
(checkHalf(str.substr(len, 1))) ? cnt++ : cnt += 2;
}
return cnt;
}
trace(getStrBytesTotal("ああああ")); //8
全角→半角変換を作ろうと思ったら、JavaScriptで公開してくれている人がいました。
AOK - 全角英数字・記号を半角に置換
半角カナの変換は1文字ずつ置換した方がよさそう...。
JavaScript例文辞典 - 半角カナから全角カナへ変換する
JavaScript例文辞典 - 半角カナから全角カナへ変換する(濁点等対応版)
IT用語辞典 - ASCII文字コード
Flash-ML - [flash:25713] Re:半角と全角の判定
Flash-ML - [flash:25698] タグを消去して単純なテキスト文章にする方法
GAC - Flash[6537] 半角
とほほ - 漢字コードについて
<追記>
2006/04/29 -
2005/04/07 - 「文字コード判定」という言い方は「 Shift-JIS か UTF-8 かを判断するスクリプト(?)」と思われるので、文章を少し改訂。
2005/03/17 - 全角半角変換等のリンク追加
投稿日 : 2005年02月15日 - yoshiweb - カテゴリ: ActionScript
コメント
投稿者 : remi
2005年12月28日 11時53分43秒
投稿者 : yoshiweb
書き込みありがとうございます。
横幅をぴったりあわせたいということでしたら、上のスクリプトは無視してこんな感じでいかがでしょう。
_txt.autoSize = true;
↑「_txt」はダイナミックテキストの名前に変えてください。
横幅をぴったりあわせたいということでしたら、上のスクリプトは無視してこんな感じでいかがでしょう。
_txt.autoSize = true;
↑「_txt」はダイナミックテキストの名前に変えてください。
2005年12月28日 17時06分09秒
投稿者 : remi
回答ありがとうございます!
autoSizeなるものがありましたかっ!
実はティッカーのようなものを作りたいとおもっていまして。
これでもう一度挑戦してみます。
ありがとうございます!
autoSizeなるものがありましたかっ!
実はティッカーのようなものを作りたいとおもっていまして。
これでもう一度挑戦してみます。
ありがとうございます!
2005年12月28日 18時29分27秒
投稿者 : http://tinyurl.com/y32b3xwb
When someone writes an article he/she retains the idea
of a user in his/her mind that how a user can know it.
Thus that's why this piece of writing is great. Thanks!
of a user in his/her mind that how a user can know it.
Thus that's why this piece of writing is great. Thanks!
2022年05月10日 10時39分23秒
投稿者 : http://tinyurl.com/y6jbphjt
This paragraph is genuinely a fastidious one it helps new internet people, who
are wishing in favor of blogging.
are wishing in favor of blogging.
2022年05月10日 14時05分02秒
投稿者 : http://tinyurl.com/
Thank you for the good writeup. It in fact was a
amusement account it. Look advanced to more added agreeable from you!
However, how could we communicate?
amusement account it. Look advanced to more added agreeable from you!
However, how could we communicate?
2022年05月12日 00時01分57秒
投稿者 : tinyurl.com
Hey there fantastic blog! Does running a blog similar to this require a large
amount of work? I've absolutely no understanding of programming
but I was hoping to start my own blog in the near future.
Anyways, should you have any ideas or techniques for new blog owners please share.
I understand this is off subject but I just wanted to ask.
Appreciate it!
amount of work? I've absolutely no understanding of programming
but I was hoping to start my own blog in the near future.
Anyways, should you have any ideas or techniques for new blog owners please share.
I understand this is off subject but I just wanted to ask.
Appreciate it!
2022年05月12日 00時26分39秒
投稿者 : http://tinyurl.com/yyflmt8u
Heya! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I
ended up losing many months of hard work due to no backup.
Do you have any solutions to stop hackers?
My last blog (wordpress) was hacked and I
ended up losing many months of hard work due to no backup.
Do you have any solutions to stop hackers?
2022年05月16日 21時25分13秒
投稿者 : tinyurl.com
Awesome blog! Is your theme custom made or did you download
it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out.
Please let me know where you got your design. Appreciate it
it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out.
Please let me know where you got your design. Appreciate it
2022年05月17日 03時44分26秒
remiと申します。
スクリプトはかなり初心者なのでお手柔らかにお願いできたらと思います。
半角と全角がごちゃまぜになっている外部テキストを読み込み、その文字数ぴったりの横幅のテキストボックスを作成したい次第です。
その際、文字列のバイト数を出すにはどのようにすればよいでしょうか。
宜しくお願いします。