2
我正在使用jquery進行表單驗證,我寫了一個函數來檢查電子郵件是否已經存在。當我在函數內部提醒時,我得到一個真或假的值,但是當我調用函數時,我得到一個未定義的值。一旦輸入電子郵件地址,我需要一個真值或假值,有人可以幫我嗎?獲取未定義的值而不是真或假
我說我的代碼去撥弄:Here
HTML:
<div style="display: none;" id="dialog-form" title="Create new master admin">
<div class="validateTips">All form fields are required.</div>
<form id="target" method="post">
<fieldset>
<table width="100%">
<tr>
<td colspan="3">Please complete the form to create a new master admin (All form fields are required)</td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td width="150px">Name</td>
<td colspan="2" valign="middle">
<input type="text" name="name" id="name" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td>Surname</td>
<td colspan="2">
<input type="text" name="password" id="password" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td>Email</td>
<td width="200px">
<input type="text" name="email" id="email" size="20" class="text ui-widget-content ui-corner-all" />
</td>
<td align="left"><span id="availability_status"></span>
</td>
</tr>
<tr>
<td>Cellphone</td>
<td colspan="2">
<input type="text" name="cellphone" id="cellphone" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td align="center" colspan="3">(A random username and password will be sent to the email provided)</td>
</tr>
</table>
</fieldset>
</form>
</div>
<button id="create-user">Create new user</button>
的Jquery:
$(function() {
var name = $("#name"),
email = $("#email"),
surname = $("#password"),
cellphone = $("#cellphone"),
allFields = $([]).add(name).add(email).add(surname).add(cellphone),
tips = $(".validateTips");
function updateTips(t) {
tips.text(t)
.addClass("ui-state-highlight");
setTimeout(function() {
tips.removeClass("ui-state-highlight", 1500);
}, 500);
}
// Min max Length of field
function checkLength(o, n, min, max) {
if (o.val().length > max || o.val().length < min) {
o.addClass("ui-state-error");
updateTips("Length of " + n + " must be between " + min + " and " + max + ".");
return false;
} else {
return true;
}
}
// Check empty fields
function checkEmpty(o, n) {
if (o.val() == "") {
o.addClass("ui-state-error");
updateTips("please fill in " + n + ".");
return false;
} else {
return true;
}
}
// Check email
function checkEmail() {
//if (o.val() == "") {
var email = $("#email").val();
var bValid = true;
$.ajax({ //Make the Ajax Request
type: "POST",
url: "master_admin_setup_submit.php", //file name
data: "email=" + email, //data
success: function (server_response) {
// alert(server_response);
if (server_response == '1') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="../images/not_available.png" align="absmiddle"> <font color="red">Email already used</font>');
//add this image to the span with id "availability_status"
bValid = false;
return (bValid);
} else {
bValid = true;
//alert(server_response);
return (bValid);
}
//alert(checkEmail());
//o.addClass("ui-state-error");
//updateTips("please fill in " + n + ".");
//return false;
//} else {
//return true;
//}
}
});
}
function checkRegexp(o, regexp, n) {
if (!(regexp.test(o.val()))) {
o.addClass("ui-state-error");
updateTips(n);
return false;
} else {
return true;
}
}
$("#dialog-form").dialog({
autoOpen: false,
width: 550,
modal: true,
resizable: false,
buttons: {
"Create master admin": function() {
var bValid = true;
allFields.removeClass("ui-state-error");
bValid = bValid && checkLength(name, "username", 3, 16);
bValid = bValid && checkLength(email, "email", 6, 80);
bValid = bValid && checkEmail();
alert(bValid);
bValid = bValid && checkLength(surname, "surname", 5, 16);
bValid = bValid && checkEmpty(cellphone, "cellphone");
// bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]");
//bValid = bValid && checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9");
if (bValid == true) {
$.post('master_admin_setup_submit.php', $('#target').serialize(), function (result) {
// alert(result);
if (result === "1") {
$(function() {
$("#dialog-email").dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
});
} else {
$(function() {
$("#dialog-message").dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
});
// Reset form
$('#target')[0].reset();
// Close main dialog
$("#dialog-form").dialog("close");
}
});
}
},
Cancel: function() {
$(this).dialog("close");
}
}
});
$("#create-user")
.button()
.click(function() {
$("#dialog-form").dialog("open");
});
});
當form submited時,php代碼是什麼?,不執行條件,因爲變量'bValid'永遠不是值。 – geeking