dimanche 1 mars 2015

how don't submit form if validation not passed

I have wrote a small code snippet:


html:



<label for="searchTextField">Please Insert an address:</label>
<br>
<form id = "formId" action="google.com">
<input id="searchTextField" type="text" size="50">
<input type="submit" value="is valid" id="submitId">
<form>


js:



var input = document.getElementById('searchTextField');
var options = {componentRestrictions: {country: 'us'}};

new google.maps.places.Autocomplete(input, options);
$("#formId").submit(function(e){
return editTerminal(e);
});
function editTerminal(id, e) {
function setPosition(){
console.log("logic");
};
return codeEditAddress(setPosition,e);
}

function codeEditAddress( callback, e) {
var address = document.getElementById('searchTextField').value;
var geocoder= new google.maps.Geocoder();
geocoder.geocode({ 'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//should be submit
alert('good address');
} else {
//should NOT be submit
alert('wrong address');
e.preventDefault();

}
});
}


DEMO


Now my form submits anyway. I want to achieve that it not submits if shows wrong address message.


Please, help to modify code.


Aucun commentaire:

Enregistrer un commentaire