2

オートコンプリート ボックスに入力された !@#$%^&*() などの特殊文字を検証し、「有効な名前を入力してください」というアラート ボックス メッセージを表示したい。以下は私のスニペットです:

$("#txtName").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Webservice.asmx/GetNames",
            data: "{'prefixText':'" + request.term + "'}",
            dataType: "json",
            success: function (data) {
                response($.map(data.d, function (item) {
                    return {
                        label: item.split('|')[0],
                        val: item.split('|')[1]
                    }
                }))
            },
            error: function (result) {
                ServiceFailed(result);
            },
            failure: function (response) {
                ServiceFailed(result);
            }
        });
    },
    select: function (event, ui) {
        autoName(ui.item.val, ui.item.label);
    },
    minLength: 4
}).data("autocomplete")._renderItem = function (ul, item) {
    var newText = String(item.value).replace(
            new RegExp(this.term, "gi"),
            "<span class='ui-state-highlight'>$&</span>");
    return $("<li></li>")
        .data("item.autocomplete", item)
        .append("<a>" + newText + "</a>")
        .appendTo(ul);
}

どんな助けでも大歓迎です。

前もって感謝します

4

1 に答える 1

1

テキストボックス内の特殊文字のブロックに関するこの質問を参照してください。

おそらく、リクエストを送信する前に検証を設定できます。すべてを実行する検証プラグイン全体を設定する代わりにkeypress

$.ajax({  
    url : url, //blah blah
    beforeSend : function(){
      if(!validateChars()){
        return false;
      }
    }
});

また

ブロックする代わりに特殊文字を取り除くことで、リクエストを送信できます。(より使いやすい)

var alowedChars = stringToReplace.replace(/[^\w\s]/gi, '')

参照用に関連コードを貼り付けました

于 2012-08-08T09:53:33.520 に答える