• ベストアンサー

テキストボックスの制限

いつもありがとうございます。 テキストボックスがあって、最大20文字しか入らないように制限をしています。 Formで次の画面へ飛ばす時、条件として * 小文字のカタカナは全角のカタカナに * 「ー」は「-」 にしたいです。 しかも3文字しかテキストボックスに入らなければ、残りの17文字は空白にして20文字の文字としておくりたいです。 このとき、どう書けばよいか教えて下さい。

質問者が選んだベストアンサー

  • ベストアンサー
  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.1

<form … onsubmit="convert()"> として function convert(){ str=form1.text1.value; str=str.replace(/ー/g,"-"); form1.text1.value=str; return true; } の様な感じで置換をします。 半角カナから全角カナへ一発でうまくやる方法はちょっと思いつきません。 同じように、地道にやるしかないと思います。 20文字に統一するには、 str=str+" …20個の全角スペース… "; str=str.substring(0,20-1); してやればいいです。

sunny65
質問者

お礼

ありがとうございます。 大変参考になりました。 お礼が遅くなり申し訳ありません。

その他の回答 (1)

回答No.2

半角全角の変換こういうのはどうでしょうか。 半角カタカナの回答できないようなので全角になってますが。 function HanZenChange(){ var str = document.form.text.value;  //ここは半角カタカナの配列です var han = new Array("ア","イ","ウ","エ","オ","カ","キ","ク","ケ","コ","サ","シ","ス","セ","ソ", "タ","チ","ツ","テ","ト","ナ","ニ","ヌ","ネ","ノ","ハ","ヒ","フ","ヘ","ホ", "マ","ミ","ム","メ","モ","ヤ","ユ","ヨ","ラ","リ","ル","レ","ロ","ワ","ン", "ガ","ギ","グ","ゲ","ゴ","ザ","ジ","ズ","ゼ","ゾ","ダ","ヂ", "ヅ","デ","ド","バ","ビ","ブ","ベ","ボ","パ","ピ","プ","ペ", "ポ","ヲ","ァ","ィ","ゥ","ェ","ォ","ャ","ュ","ョ","ッ","ー"); var zen = new Array("ア","イ","ウ","エ","オ","カ","キ","ク","ケ","コ","サ","シ", "ス","セ","ソ","タ","チ","ツ","テ","ト","ナ","ニ","ヌ","ネ", "ノ","ハ","ヒ","フ","ヘ","ホ","マ","ミ","ム","メ","モ","ヤ", "ユ","ヨ","ラ","リ","ル","レ","ロ","ワ","ン","ガ","ギ","グ", "ゲ","ゴ","ザ","ジ","ズ","ゼ","ゾ","ダ","ヂ","ヅ","デ","ド", "バ","ビ","ブ","ベ","ボ","パ","ピ","プ","ペ","ポ","ヲ","ァ", "ィ","ゥ","ェ","ォ","ャ","ュ","ョ","ッ","ー"); while(str.match(/[ヲ-ン]/)){ //ここも半角です for(var i = 0; i < han.length; i++){ str = str.replace(han[i], zen[i]); } } document.form.text.value = str; }

sunny65
質問者

お礼

ありがとうございます。 大変参考になりました。 お礼が遅くなり申し訳ありません。

関連するQ&A