1

テキストボックスの値を制限する際に問題に直面しています。

Textbox は、 以外のシンボルを許可してはなりません"?*,"

元:A123?*,B456?*,C789?*,D126?*,E?*666

コンマの前後に(,)のみ10 Characters/Symbols/Numbers使用できます。

54 charactersコンマを含むテキストボックス内でのみ許可されます。

4 comma'sTEXTBOX 内でのみ許可されます。

可能なテキストボックス入力: ABC?12345*,A?BC*12345,A???1234**,?*C?12345*,ABC?123***

また、記号と文字は置き換え可能です。開始時に記号が来るか、文字または数字のいずれかです。

詳しくは画像をご確認ください。 ここに画像の説明を入力

他の記号と許可されたコンマで制限しようとしましたが、10文字で制限し、記号と文字数を許可するのに苦労しています。

//<![CDATA[ 
window.onload = function() {
    /*var f = document.getElementById('textbox_restrict');
     f.addEventListener('keyup', function(evt){
     alert("Vinayagam");
     var regex = /[^a-zA-Z0-9]/;
     if(regex.test(this.value)) {
     this.value = this.value.replace(regex, '')  
     }
     });*/
    $('#textbox_restrict').keypress(function(e) {
        $('#backgroundPopup').animate({'opacity': 0.0, 'display': 'none'});
        this.value = this.value.replace(/[^0-9a-zA-Z\*\?\{0,12}]/g, '').toUpperCase();
    });

}//]]>  

$(function() {
    $('body').on('keyup', 'input[id^="textbox_restrict"]', function() {
        this.value = this.value.replace(/[^0-9a-zA-Z\*\?]/g, '').toUpperCase();
    });

    $('#search').live('click', function() {
    }); // End of save click function
}); // End of function 

私もこの質問を参照しましたが、シナリオは異なります: C# のテキストボックスを数字と (ドット "." またはコンマ ",")、"." の後にのみ受け取るように制限する方法 または "," は 2 つの数字のみを許可します

正規表現 数字と小文字以外のものを置き換えます

jQuery keyup() 不正な文字

親切にアドバイスしてください! 私はそれを実装するのに少し混乱しました。

4

2 に答える 2

1

すべてのことを一度に終わらせようとしないほうがいいかもしれません。各段階を簡素化するために、タスクを細かく分割しました。以下のスニペットをイベントハンドラーとして追加するkeyupだけoninputですinput

keyUp = function () {
    var n, parts, temp,
        text = this.value,
        caretPos = this.selectionEnd; // Stores the caret position
    parts = text.split(','); // Creates an array of comma separated values
    while (parts.length > 5) { // Limits the array length to 5 i.e only 4 commas allowed
        parts.pop();
    }
    for (n = 0; n < parts.length; n++) { // Iterates through each comma separated string
        parts[n] = parts[n].substring(0, 10); // Limits the string length to 10
        temp = parts[n].match(/[0-9a-zA-Z\*\?]/g); // Creates an array of acceptable characters in the string
        if (temp) {
            parts[n] = temp.join(''); // Adds the accepted value to the original
        } else { // If not value, add empty to the original
            parts[n] = '';
        }
    }
    this.value = parts.join(',').toUpperCase(); // Creates a new acceptable value to the input
    this.setSelectionRange(caretPos, caretPos); // Returns the caret to the original position
    return;
}

jsFiddleでのライブ デモ。

編集

文字列の途中に文字を追加するときに、文字列の末尾にある文字が誤って削除されないように、54 への文字列抑制を削除しました。else許容値が見つからない場合に空の文字列を追加する追加。またoninput、このコードを使用すると、より良いイベントになるようです。

編集Ⅱ

途中の文字列を編集すると元の位置に戻るキャレット位置を追加しました。マウスでテキストを貼り付けるときは完璧ではありませんが、キーボード入力では機能するようです。(フィドル リンクが更新されます。)

于 2013-06-18T19:31:37.497 に答える