Java Reference
In-Depth Information
{
alert(“Please enter a user name to check!”);
return false;
}
request.url = “formvalidator.php?username=” + userValue;
request.callBack = checkUsername_callBack;
request.send();
if (isUsernameTaken)
{
alert(“The username “ + userValue + “ is not available!”);
return false;
}
//Now check the email
var emailValue = document.getElementById(“email”).value;
if (emailValue == “”)
{
alert(“Please enter an email address to check!”);
return false;
}
request.url = “formvalidator.php?email=” + emailValue;
request.callBack = checkEmail_callBack;
request.send();
if (isEmailTaken)
{
alert(“I'm sorry, but “ + emailValue + “ is in use by another user.”);
return false;
}
//If the code's made it this far, everything's good
return true;
}
The e-mail-checking code goes through the same process that was used to check the user name. The
value of the
Email
fi eld is retrieved and checked to determine whether the user typed anything into
the text box. Then that value is used to make another request to the server. Notice again that the
url
and
callBack
properties are explicitly set. If
isEmailTaken
is true, an alert box shows the user that
another user has taken the e-mail address and the function returns
false
. If the address is available,
the function returns
true
, thus making the browser submit the form.
Chapter 15
Exercise 1 Question
Modify the answer to Chapter 14's Question 2 using jQuery. Also add error reporting for when an error
occurs with the Ajax request.