Connecting Tech Pros Worldwide Forums | Help | Site Map

Ajax Server Validation Question

John Chan
Guest
 
Posts: n/a
#1: Nov 21 '06
Hi,

Im doing a maintenance application in ajax and coldfusion at work on IE6
exclusively. I have a save button on each form and i have to do various
validations server side and on client side when the user clicks save i.e
check that user exists in table, check users password is the same as
password in table, check password is valid, check that logged in user is a
super user etc etc to get the correct error message. Different forms may or
may not use all of these validations and not always in the same order. At
the moment for each form, im calling the first validation when user hits
save, then in that callback function im checking to see if its been
validated, if it has then run the next validation function and so on, so its
like a cascading effect. Is there a better way of doing this? so i can reuse
code on different forms. i.e. something like the following simplified code
where validation1() and validation(3) are server side validations using
ajax.

function save(){

// check validation one

if(!validation1()){

return false;

}

// check validation two

if(!validation2()){

return false;

}

// check validation three

if(!validation3()){

return false;

}

// validation passed so save

ajaxRequest(someUrl, someCallback);

}

Any advice appreciated.



David Golightly
Guest
 
Posts: n/a
#2: Nov 21 '06

re: Ajax Server Validation Question



John Chan wrote:
Quote:
Hi,
>
Im doing a maintenance application in ajax and coldfusion at work on IE6
exclusively. I have a save button on each form and i have to do various
validations server side and on client side when the user clicks save i.e
check that user exists in table, check users password is the same as
password in table, check password is valid, check that logged in user is a
super user etc etc to get the correct error message. Different forms may or
may not use all of these validations and not always in the same order. At
the moment for each form, im calling the first validation when user hits
save, then in that callback function im checking to see if its been
validated, if it has then run the next validation function and so on, so its
like a cascading effect. Is there a better way of doing this? so i can reuse
code on different forms. i.e. something like the following simplified code
where validation1() and validation(3) are server side validations using
ajax.
>
function save(){
>
// check validation one
>
if(!validation1()){
>
return false;
>
}
>
// check validation two
>
if(!validation2()){
>
return false;
>
}
>
// check validation three
>
if(!validation3()){
>
return false;
>
}
>
// validation passed so save
>
ajaxRequest(someUrl, someCallback);
>
}
>
Any advice appreciated.

Well, yes and no. You could go there and have your Ajax call do your
form validation when you click the save button, but then your problem
will be the time it takes for that call to go over the wire. So it
really depends on your (and more importantly, your users) connection
speed, but basically you're making 2 round-trips to the server for this
- the first (XMLHttpRequest with the entire form contents) taking a
second or two at least. The savings vs. reloading the page will be a
lot less than pure client-side validation.

I personally prefer to peg my Ajax validation calls to individual input
"blur" events. Once an input field loses focus (onblur), go ahead and
launch the XHR to validate the field while the user is typing in the
next field. Hopefully then the last field in your form will be an
optional one that doesn't require validation. In this manner, by the
time the user reaches the "Save" button, the validation will hopefully
be completed, and the total time spent will be little more than using
pure client-side validation.

Give that a try and let us know how it goes.

-David

Closed Thread


Similar JavaScript / Ajax / DHTML bytes