我正在尋找JavaScript和jQuery專家來設置正確的路徑。我有以下代碼,我注意到在函數完成之前,代碼通過focus()並返回false;並且一旦函數返回false,有人會告訴我寫這段代碼的正確方法嗎?謝謝!JavaScript的最佳代碼實踐if-then-else
// validate passwords
if(!VerifyPassword($("#CurrentPassword").val())) {
$("#CurrentPassword").focus();
return false;
}
if($("#NewPassword").val() != "") {
if(!ValidatePassword($("#NewPassword").val())) {
$("#NewPassword").focus();
return false;
}
if($("#NewPassword").val() != $("#RetypePassword").val()) {
alert("The new password is not the same as the retyped password");
return false;
}
}
function ValidatePassword(password) {
if(password.length < 6) {
alert("Password must contain at least six characters!");
return false;
}
re = /[0-9]/;
if(!re.test(password)) {
alert("Password must contain at least one number (0-9)!");
return false;
}
re = /[a-z]/;
if(!re.test(password)) {
alert("Password must contain at least one lowercase letter (a-z)!");
return false;
}
re = /[A-Z]/;
if(!re.test(password)) {
alert("Password must contain at least one uppercase letter (A-Z)!");
return false;
}
return true;
}
function VerifyPassword(password) {
urlString = "../sql/db_verify_password.php?Password=" + password;
/* send calendar updated information and return status message */
$.ajax({
type: "GET",
url: urlString,
dataType: "xml",
success: function(xml) {
$(xml).find('Results').each(function() {
var status = $(this).find('Status').text();
if(status != "OK") {
alert(status);
return false;
}
});
}
});
}
你在期待,什麼是實際發生的?目前尚不清楚你的問題是什麼。 –
'通過焦點()'是什麼意思? – n8wrl
.focus()用於將函數綁定到焦點事件。根據你的代碼,它看起來並不像你想要做的那樣,你是否試圖設置焦點? –