Java Reference
In-Depth Information
times. Validating fi elds in a form is a sequential process, and its submission depends upon the outcome
of the onsubmit event handler. Using synchronous communication forces the function to wait for infor-
mation to be retrieved from the server before attempting to validate the fi eld. If you used asynchronous
communication, form_submit() would execute and return a value before the username and email could
be validated. Also note that the HttpRequest constructor received no arguments. This is because you
can explicitly set the url and callBack properties with the new version.
The fi rst fi eld to check is the Username fi eld.
function form_submit()
{
var request = new HttpRequest();
request.async = false;
//First check the username
var userValue = document.getElementById(“username”).value;
if (userValue == “”)
{
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;
}
//more code here
}
This code retrieves the value of the Username fi eld and checks to see whether any information was
entered. If none was entered, a message is alerted to the user informing them to enter data. If the user
entered information in the Username fi eld, then code execution continues. The url and callBack prop-
erties are assigned their values and the request is sent to the server. If it turns out that the user's desired
user name is taken, an alert box tells them so. Otherwise, the code continues to execute and checks the
e-mail information.
function form_submit()
{
var request = new HttpRequest();
request.async = false;
//First check the username
var userValue = document.getElementById(“username”).value;
if (userValue == “”)
Search WWH ::




Custom Search