我有一个HTML表单,我用两个JS函数验证:emptyValues()
和checkReg()
,它们位于一个单独的JS文件中。第一个功能完美,但我遇到第二个问题。
在表单提交时,它会检查每个字段并在出现错误时发出警报。我希望该功能首先检查登录可用性,稍后再继续检查密码是否相等以及电子邮件是否正确。但该功能仅在出现其他错误时显示登录警报,并且仅在其他警报之后显示。似乎当密码和电子邮件正常时,它不会检查登录(或没有时间给出答案)。怎么可能?
这是我的代码:
HTML表单
<form id="signup_form" action="" method="post" onsubmit="return (emptyValues(this,\'signup_form\') && checkReg(this));">
<input id="login" name="login" type="text" placeholder="Login" />
<input id="password" name="password" type="password" placeholder="Password" />
<input id="password2" name="password2" type="password" placeholder="Repeat password" />
<input id="email" name="email" type="text" placeholder="E-mail" />
<input class="button" id="signup" name="signup" type="submit" value="Sign up" />
<input name="who" type="hidden" value="'.$who.'" />
</form>
JS功能
function checkReg(form) { // checks registration form - checks if login is available, passwords are equal and email is correct
if (form.login.value && form.password.value && form.password2.value && form.email.value) {
var login = form.login.value;
var password = form.password.value;
var password2 = form.password2.value;
var email = form.email.value;
$.post("php/checkUsername.php", { login: login }, function(result){
if (result == 1) {
// does nothing
} else {
alert("This login is not available");
form.login.value = '';
return false;
}
});
if (password != password2) {
alert("Passwords are different! Please try again!");
form.password.value='';
form.password2.value='';
return false;
}
var TestEmail = /^(.[A-Za-z0-9\-]*\w)+@+([A-Za-z0-9\-]*\w)+(\.[A-Za-z]*\w)+$/;
var Result = email.match(TestEmail);
if (Result == null) {
alert("Please, type your e-mail correctly!");
form.email.value='';
return false;
}
return true;
}
PHP文件,用于检查登录可用性的AJAX函数进行响应
<?php
require_once('../p4gw78/kx96hf_localhost');
mysql_connect('localhost', $uzytk23, $pvd32);
mysql_select_db($baza);
$login = $_POST['login'];
$result = mysql_query("SELECT * FROM `f_users` WHERE `u_login` = '$login'");
if(mysql_num_rows($result) > 0) {
echo 0;
}
else {
echo 1;
}
?>