Java Reference
In-Depth Information
form should submit its data only when the form fi elds have been validated. Therefore, the onsubmit
event handler is set to return form_submit(). The form_submit() function returns either true or
false, making the browser submit the form's data if everything is okay and not submit if a fi eld is not
validated.
The JavaScript code holds the most changes; in this new implementation, two global variables, called
isUsernameTaken and isEmailTaken, are declared. These variables hold true or false values: true
if the user name or e-mail is taken, or false if it is not.
var isUsernameTaken;
var isEmailTaken;
function checkUsername_callBack(sResponseText)
{
if (sResponseText == “available”)
{
isUsernameTaken = false;
}
else
{
isUsernameTaken = true;
}
}
function checkEmail_callBack(sResponseText)
{
if (sResponseText == “available”)
{
isEmailTaken = false;
}
else
{
isEmailTaken = true;
}
}
The fi rst two functions, checkUsername_callBack() and checkEmail_callBack(), are somewhat
similar to their original versions. Instead of alerting information to the user, however, they simply assign
the isUsernameTaken and isEmailTaken variables their values.
The function that performs most of the work is form_submit(). It is responsible for making the
requests to the server and determines if the data in the form fi elds are ready for submission.
function form_submit()
{
var request = new HttpRequest();
request.async = false;
//more code here
}
This code creates the HttpRequest object and sets it to synchronous communication. There are times
when synchronous communication is appropriate to use, and during form validation is one of those
Search WWH ::




Custom Search