简体   繁体   English

验证自动完成

[英]Validating auto complete

Trying to the change fuction to validate my auto complete text box 尝试更改功能以验证我的自动完成文本框

So far my auto complete works with this following code: 到目前为止,我的自动完成功能可以使用以下代码:

Autocomplete code: 自动完成代码:

$("#Form_Ser_Num").autocomplete({
        source: "@Url.Action("SerialProdNumStockSiteAutoComplete", "Ajax")?stocksitenum=LW&model=" + $("#Form_Prod_Num").val(),

        minlength: 2,
        delay: 300,
        select: function (event, ui) {
            event.preventDefault();
            event.target.innerText = ui.item.label.split(',')[0];
            $("#Form_Ser_Num_Source").val(ui.item.label.split(',')[1]);
        }
    });

I want to add this so it validates: 我想添加它,以便验证:

change: function( event, ui ) {
    if ( !ui.item ) {
        var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i" ), valid = false;

        select.children("option").each(function() {
            if ($(this).text().match(matcher)) {
                this.selected = valid = true;
                return false;
            }
        });

        if (!valid) {
            // remove invalid value, as it didn't match anything
            $(this).val("");
            select.val("");
            input.data("autocomplete").term = "";
            return false;
        }
    }
}

I have found this online, but i cant implement it to my code. 我在网上找到了这个,但是我无法在我的代码中实现它。

Any ideas how i can use this change function to my auto complete function. 我如何使用此更改功能到我的自动完成功能的任何想法。

Assuming that you want to check/validate your text field value with the source, here is something that you can give it a try. 假设您想使用源代码检查/验证文本字段值,可以尝试一下。 Just modify the change and reponse functions as follows: 只需修改更改和响应功能,如下所示:

change:function(event, ui){
  if (!ui.item){
    $(this).val('');
    }
  }

response: function(e, ui) {
  if (ui.content.length == 0) {
    $(this).val("");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM