I have the validation for autocomplete working.
http://skoizumi.com/autocomplete/index.html
var ac = $( "#searchtext" ).autocomplete({
source: "testdata.php",
minLength: 2,
select: function( event, ui ) {
console.log( '3');
},
close: function( event, ui ) {
console.log( 'closing.' );
}
});
jQuery.validator.addMethod("contractorName", function (value, element) {
var result = false;
var contractorName = $("#searchtext" ).val();
//validating the specified field has a valid name
$.ajax({
type: "GET",
url: "/autocomplete/return2.php",
async: false,
dataType: "json",
data: {
'searchtext': $( "#searchtext" ).val()
},
complete: function(test){
res = JSON.parse( test.responseText );
console.log( res );
result = res.result;
}
});
return result;
}, "Specified Contractor doesn't exist");
I made it so that when you select “European Nightjar” from the list or type the same thing, it should validate and return true, otherwise it should display an error message saying “Specified contractor doesn’t exist.”
However, if you have invalid data in the textbox and select from the list it returns error because validation takes place before selecting from the list.
Is there a way so that validation doesn’t have to happen when selecting from the list?
this is from the jQuery demo site:
add a different function for select: , select fires when you choose from dropdown ,
instead of just putting console.log( ‘3’); validate the text here