[jQuery] Validation and autocomplete
Hi,
I am trying to use validation plugin found at http://docs.jquery.com/Plugins/Validation
but cannot get it to work when there is a autocomplete on the page.
It validates even when a value is picked from the suggested values
rather then only on submit.
I am new to jquery, but here is how it looks like. Any help would be
appreciated.
<script type="text/javascript">
function findValue(li) {
if (li == null) return "";
if (!!li.extra) var sValue = li.extra[0];
//set the value of the hidden input field so we don't have
to lookup the bid Id on the server again
$("#<%=bidId.ClientID %>").val(sValue);
}
function selectItem(li) {
findValue(li);
}
function formatItem(row) {
return row[0];
}
function lookupBidId() {
var oSuggest = $("#policyNumber")[0].autocompleter;
oSuggest.findValue();
return false;
}
jQuery(function($) {
//attach autocomplete to bidNumber text box
$("#policyNumber").autocomplete("ws://../WebServices/
BidSearch.asmx/GetQuickBidSearchResults", {
delay: 10,
minChars: 6,
matchSubset: 1,
matchContains: 1,
cacheLength: 10,
onItemSelect: selectItem,
onFindValue: findValue,
formatItem: formatItem,
selectFirst: true,
selectOnly: true,
maxItemsToShow: 25
});
$.validator.addMethod("bidFound",
function(value) {
debugger;
return $("#<%=bidId.ClientID
%>").val() != "";
},
"Invalid policy number.");
//do not validate on each key up
$("#policyNumber").validate({onkeyup:false});
// validate form on submit
$("#aspnetForm").validate(
{
valgroups : {
search : { buttons : [ '<%= bidDetail.ClientID
%>' ], fields : [ "policyNumber" ] }
},
rules:
{
policyNumber:
{
required: true,
bidFound: true
}
},
messages: {
policyNumber: {
required: " Please enter policy number in
format {bid number}-{client number}-{option number}"
}
}
});
});
</script>