3

サインアップ時にユーザー名をクリーンアップするためにJavaScriptコードを使用します。文字が許可されていない場合は、ダッシュに置き換えられます。

私の問題は、ユーザーがテキストの途中に文字を追加したい場合、カーソルが入力の最後に自動的に置かれることです。

ここでテストできます:http://jsfiddle.net/tZv5X/

HTML:

Username : <input type="text" id="username" />​

JS:

// Clean username
function clean_username(s) 
{
    var temp = s.replace(/[àâä@]/gi,"a");
    temp = temp.replace(/[éèêë]/gi,"e");
    temp = temp.replace(/[îï]/gi,"i");
    temp = temp.replace(/[ôö]/gi,"o");
    temp = temp.replace(/[ùûü]/gi,"u");
    temp = temp.replace(/[ç]/gi,"c");
    temp = temp.replace(/[. _,;?!&+'"()]/gi,"-");
    return temp;
}

var current_value;
$("#username").keyup(function(e)
{
    if($(this).val() != current_value)
    {
        $(this).val(clean_username($(this).val()));
        current_value = $(this).val();
    }
});​

何か案が ?

ありがとう

4

2 に答える 2

1

それを行うための良い方法はありませんが、プラグイン、jQueryのcaretの位置はそれからいくつかの苦痛を取り除く必要があります。

于 2012-11-22T15:10:38.910 に答える
1

jCaretプラグイン( @puppybeardでも言及されています)を使用できます。見てみな:

$("#username").bind({
    keydown: function() {
        var $this = $(this);
        $this.data("pos", $this.caret().start);
    },
    keyup: function() {
        var $this = $(this),
            pos = $this.data("pos"),
            value = clean_username(this.value);
        if (value !== this.value) {
            this.value = value;
            $this.caret(pos + 1, pos + 1);
        }
    }
});​

デモ:http: //jsfiddle.net/tZv5X/3/

于 2012-11-22T15:22:08.880 に答える