Question

I have a text input field that is set to auto complete for the user using 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.

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top