Java Reference
In-Depth Information
alert(“Please enter a user name to check!”);
return;
}
var url = “formvalidator.php?username=” + userValue;
var request = new HttpRequest(url, checkUsername_callBack);
request.send();
}
Use the document.getElementById() method to fi nd the <input/> element and use the value prop-
erty to retrieve the text typed into the text box. Then check to see if the user typed any text by comparing
the userValue variable to an empty string ( “” ). If the text box is empty, the function alerts the user to
input a user name and stops the function from processing further. The application would make unnec-
essary requests to the server if the code didn't do this.
Next construct the URL to make the request to the PHP application and assign it to the url variable.
The fi nal steps in this function create an HttpRequest object, pass the URL and the callback function
to the constructor, and send the request.
The checkUsername_callBack() function executes when the HttpRequest object receives a complete
response from the server. This function uses the requested information to tell the user whether the user
name is available. Remember, there are two possible values sent from the server, available and not
available ; therefore, you only need to check for one of these values.
function checkUsername_callBack(sResponseText)
{
var userValue = document.getElementById(“username”).value;
if (sResponseText == “available”)
{
alert(“The username “ + userValue + “ is available!”);
}
else
{
alert(“We're sorry, but “ + userValue + “ is not available.”);
}
}
If the server's response is available, the function tells the user that their desired user name is okay to
use. If not, the alert box says that his user name is taken.
Checking the e-mail's availability follows an almost identical process. The checkEmail() function
retrieves the text typed in the Email fi eld, and passes that information to the server application.
function checkEmail()
{
var emailValue = document.getElementById(“email”).value;
if (emailValue == “”)
{
alert(“Please enter an email address to check!”);
return;
}
var url = “formvalidator.php?email=” + emailValue;
Search WWH ::




Custom Search