我正在使用 Google Phone 验证库来检查号码是否对 AJAX 调用有效。如果号码无效,我需要以下函数来阻止表单提交。
目前它会阻止表单正确提交,但是当它返回 true 时表单不会提交。确实发生了真实声明的警报,但表单仍然没有提交。我错过了什么?
$(document).ready(function(){
$('form#sellerForm').submit(validatePhoneNumber);
});
function validatePhoneNumber() {
var output = null;
var phoneNumber = $('#phone').val();
var node = document.getElementById('phoneOutput');
var country = $('#country').val();
if(country == -1){
output = translateNoCountry();
node.innerHTML = output;
event.preventDefault();
return false;
}
if(output == null){
//AJAX to get country code from country for var regionCode;
$.ajax({ type: "POST",
url: '/ajax/validate_phone.php',
data: {country: country},
cache: false,
success: function(result) {
var regionCode = jQuery.parseJSON(result);
try {
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
var isNumberValid = phoneUtil.isValidNumber(number);
if(!isNumberValid){//invalid number
output = translateInvalidePhone();
}
} catch (e) {
output = translateInvalidePhone();
}
if(output != null){
node.innerHTML = output;
event.preventDefault();
return false;
}else{
alert('Hello world');
return true;
}
}
});
event.preventDefault();
return false;
}
}