I have this code that runs when the form is submitted:
$("form").submit(function (e) { var geocoder = new google.maps.Geocoder(); var address = document.getElementById("Address").value; geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { $("#LatitudeLongitude").val(results[0].geometry.location); $("form").submit(); } else { alert("Geocode was not successful for the following reason: " + status); } }); $('form').unbind('submit'); return false; });
What he does: he calls the google geocoding service to translate the address to latitude / longitude, which is set in the hidden form field. If there is a result, then the form is submitted.
The problem is that if the check fails (for example, a required field has not been set), the geocoding call is still running. Moreover, if I click the submit button a second time, even if the required field has not been set, the form will be published.
How can I call the geocoding service only if the unobtrusive check is successful?
validation asp.net-mvc asp.net-mvc-3 unobtrusive-validation
Nicolas cadilhac
source share