简体   繁体   English

jQuery验证引擎-针对JSON列表进行验证

[英]jquery validation engine - validate against json list

I have a text input field that is set to auto complete for the user using ajax/php. 我有一个文本输入字段,该字段设置为使用ajax / php为用户自动完成。 Although the user is allowed to enter any entry, I would like to warn the user if they try to enter a value that is not pre-existing. 尽管允许用户输入任何条目,但是我想警告用户如果他们尝试输入不存在的值。

<input type="text" id="customerName" size="50" class="validate[required]" />
<input type="hidden" id="customerid"/>

Now I have an auto complete against that field: 现在我有一个自动完成该领域:

$("#customerName").autocomplete({
    source: function (request,response) {
        $.ajax({
            url: 'getData.php?f=c&s=112',
            dataType: 'json',
            data: {
                term: request.term,
                country: $("#country").val()
            },
            success: function (data) {
                response(data)
            }
        })
    },
    minLength: 2,
    delay: 0,
    dataType: 'json',
    select: function( event, ui ) {
        $("#customerid").val(ui.item.id);
    }
});

I tried using blur() but it goes off on too many instances, so I figured I would attach to the validation engine's hook and check to see which field is being tested and so I could test to see if the user entered something new or not by testing customerid being empty or not. 我尝试使用blur(),但是它在太多实例上都失败了,所以我认为我将附加到验证引擎的钩子上,并查看正在测试的字段,因此可以测试用户是否输入了新内容通过测试customerid是否为空。

form.bind('jqv.field.result', function(event, field, errorFound, promptText) {
    console.log('event = ' + event.toSource());
    console.log('field = ' + field.toSource());
    console.log('errorFound = ' + errorFound);
    console.log('promptText = ' + promptText);

I can't seem to figure out how to test the field param to see if I'm in the right space or not. 我似乎无法弄清楚如何测试字段参数,以查看我是否在正确的空间中。

If you can think of an easier/different method feel free to post as well, this was just my initial idea. 如果您也可以随意考虑发布一种更简单/不同的方法,那只是我的最初想法。

I guess I just needed a good night sleep. 我想我只需要睡个好觉。 It's a pretty simple solution: 这是一个非常简单的解决方案:

form.bind('jqv.field.result', function(event, field, errorFound, promptText) {
    if (!errorFound)
        if (field.attr('name') === .... 

if there is an error, let the normal thread continue because there is a local error. 如果有错误,则由于存在本地错误,请让普通线程继续。 Otherwise, we can check on the name field to see if it's the field that we want to trigger at, and do. 否则,我们可以检查名称字段以查看它是否是我们要触发并执行的字段。

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

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