Я хочу проверить форму, чтобы я мог проверить основы (например, имя пользователя, имя пользователя, минимальную длину и т. Д.), Но также, чтобы я мог гарантировать, что такие вещи, как имя пользователя и адрес электронной почты, еще не были (что требует поиска в базе данных) ,
У меня есть все части, и после игры они сузили его, чтобы найти, что инструкция $. Post
, в то время как она предназначена для добавления в счетчик «fail», не делает этого. Как я могу получить какую-то форму вывода из строки $. Post
, чтобы я мог действительно подтвердить (потому что прямо сейчас, даже если имя пользователя недоступно, оно отправит форму).
Here is my <script>
(although for simplicity shortened to just show the username piece). Note that 'usernameoff' is my error block:
function validateForm()
{
//set variables
var username=document.forms["registrationnew"]["username"].value;
var usernamespace=username.indexOf(" ");
//set fail counter variable to determine if any requirements failed
var counter=0;
//check that username isn't blank, containing spaces, too short, or taken
if (username==null || username=="")
{
counter++;
$('#usernameoff').html("Must enter a username");
}
else if (username.indexOf(" ") >= 0)
{
counter++;
$('#usernameoff').html("Cannot contain spaces");
}
else if (username.length < 3)
{
counter++;
$('#usernameoff').html("Must be at least three characters");
}
else
{
$.post("check_username.php", {username: username}, function(result)
{
if(result == 0)
{
$('#usernameoff').html("Available");
}
else
{
counter++;
$('#usernameoff').html("Not available");
}
});
}
//if any test failed, return false
if (counter > 0)
{
document.getElementById("errorflag").innerHTML=counter;
return false;
}
}
$.post
conducts a query to determine whether or not the username entered has already been taken (and returns 0 or 1). My issue is that, regardless of which it returns, it does not increment 'counter', and therefore the form still submits.