For my page I was using server-side validation. In order to reduce post-backs I decided to implement client-side validation -- do the same validation as server (but keep server-side for security reasons). I could not find the answer to this easily -- Does my client-side validation stop post-backs? I've noticed that having one client-side validation does not stop the other server-validations from doing a post-back. Currently I have 5 CustomValidators, all with ClientValidationFunction and OnServerValidate set. When the client script sets args.isValid as false, does it stop the postback from occuring? I could not find an easy way to figure this out. Thanks,
Chris
![]() |
0 |
![]() |
Hi Chris,
Try this post http://forums.asp.net/t/1410290.aspx.
Hope that helps.
Aaron
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
![]() |
1 |
![]() |
Yes, args.IsValid=false, causes that validator to be invalid on the client side. This should stop all postbacks from controls in the save ValidationGroup. Also, you need to set the CausesValidation property to true on the postback controls that you want to validate before posting back. Hope this helps.
When I answer your question successfully, you are helping me grow by asking it.
![]() |
0 |
![]() |
Aaron,
Thank you for the link, but it does not answer my question. They mention that "client side is executed, then server side" which I know -- but my question is if client-side fails, does the post-back still occur causing server-side to kick in, and how this works with multiple validators.
Ivan,
Thanks for the reply - that is what I wanted to know; it's probably good idea to group my validators.
![]() |
0 |
![]() |
Hi Chris,
Yes, if any client side validation fails it stops the page from being posted to the server.
Therefore preventing, useless posts to the server, and giving the user a quicker feedback on the corrections needed.
Now, once all the client-side validators are valid. Then the page is posted to the server…and then all the server side validators fire, as they should, to double check the validation and for obvious security reasons as you mentioned.You say in you question :
“I noticed that having one client-side validation does not stop the other server-validations from doing a post-back”.
Regarding Custom Validators…If you want them to do a client side check, you need to write custom Javascript for them.
If there is no custom Javascript for them…then obviously they will post to the server, without doing a client check...
If you did write a custom JS function and it’s not picking up on wrong input...that means that your JS function is not properly written…
Did you write custom JS for your Custom Validators?
To be clear, client side validation will not prevent a post-back persee…it prevents
postback if the client validation is failed. Once all is passed…then a post-back occurs, anways...for server side check...as it always should!
So in your case…when you submit the form…obviously all the client side validation comes out as valid…and then it’s posted to the server…if your custom validators don’t have custom JS function associated with them…then they don’t do client side validation and post straight to the server…I think this is what is happening, in your case…let me know.
Regards,
- Joel
"The truth is rarely pure and never simple."
- Oscar Wilde, The Importance of Being Earnest
![]() |
0 |
![]() |
langjoel,
Thank you for your response. Regarding my comment, "I noticed that having one client-side validation does not stop the other server-validations from doing a post-back"
I have valid JavaScript checking functions. What I was referring to, when I first implemented client-side validation, I only had 1 javascript validation for one control. The rest still had server-side validation. When the one control with client-side had invalid entry, and I clicked submit, all validation errors appeared, not just the one with my client-side validator. This is the reason for my initial inquiry, on whether client-side validation will stop post-backs (as clearly it wasn't stopping the post-backs in this example). My validators setup is as follows:
5 CustomValidators, with no ControlToValidate attribute set, with no ValidatorsGroup attribute set, but with both ClientValidationFunction and OnServerValidate attributes set. When I added them all to the same ValidatorsGroup, validation stopped altogether. I had some issues when setting the ControlToValidate, as one of the controls is represented as a label which is updated by the server after a web-service is called. I now have all 5 CustomValidators set for both client and server validation, and wanted to make sure that my client-side validations would stop unneccesary post-backs.
I cannot verify this, but this is what I am assuming happens with my setup: Client-side validations are called one by one, independent of eachother. If a client-side fails, nothing will happen (other than error message), and moves on to next client-validation. If at least one of the client-side validations sets isValid = true, then after all client-side validations run, a post-back will be made. At this point server-validation occurs and proceeds from there. Is this assumption correct?
![]() |
0 |
![]() |
As a test I removed the OnServerValidate fields, and having done that, no validations are performed. Apparently my Client-side validations aren't doing much. Here is one of my javascript validators:
1 function ValidateEvaluatee(sender, args) { 2 if (ctlEvaluateeLbl == null || ctlEvaluateeLbl.innerHTML == "[Please select a Person]") { 3 args.isValid = false; 4 } 5 else { args.isValid = true; } 6 }And the corresponding aspx CustomValidator:
1 <asp:CustomValidator runat="server" ErrorMessage="Please select a valid user for feedback." CssClass="validation" Display="Dynamic" ID="LblEvaluateeValidator" ClientValidationFunction="ValidateEvaluatee">*</asp:CustomValidator>
![]() |
0 |
![]() |
Yeah,
What you are assuming makes much sense. I never had the same situation as you have now, but, it seems that each Javascript function is overwritting the args.isvalid property, as they each run seperately.
So if one functions sets the args.isvalid =false...then the next function will overwrite it to args.isvalid =true.
So as a matter of fact, the only Javascript funtion that actually determines the behavior of the page, is the the last one that runs...
You can get around this problem, by adding a bit of logic to each of your Javascript functions to check if args.Valid has "arrived" to it false already...
As an example:
function validateLength(oSrc, args){
if (args.isValid =False) {
// Do nothing, args.isValid remains false
}
else
{
// Check for the validity of lenght and set Args.isValid to appropirate Value
}
}
So by adding logic that check the status of the Args.isValid at the beging of your JS functions,.if ever the previous function as determined the validation was false...it will leave it false...!
Regards,
- Joel
P.S I don't know how if the grammar of this Javascript function is accurate (I don't often write JS)...it was to give you an idea of the logic to follow...
"The truth is rarely pure and never simple."
- Oscar Wilde, The Importance of Being Earnest
![]() |
0 |
![]() |
I do not think it is the case that each javascript function is passed the same isValid (ie - they do not overwrite eachother), though I could be wrong. I just tried your suggestion (correcting some of the javascript grammar), and it doesn't not change anything - no validations take place.
![]() |
0 |
![]() |
Ok My Client-Side validation is working now - It turns out that the javascript variable to use is arg.IsValid and not arg.isValid. Needs to have a capital 'I' ;)
Going back to my original question however, I would like to know how client-side validation works with multiple groupped or non-groupped controls, and under which conditions the post-back is stopped or allowed.
![]() |
0 |
![]() |
I read the comment you just posted after my last reply.
.1 You need to keep the OnServerValidate fields...otherwise Asp.net does not know which server side function to call when doing the validation....
.2 For custom validators you don't need to explictely put a "control do validate", since in your custom validation function you can reference anything you like.
.3 Is there a reason why you are using validation groups?
Is you want everything validated on the same page...there is no point of using validation group...
Validation groups are useful if you want some controls validated and anothers not...this maybe explains why when you put things in a validation group it din't check for validity.
This from MSDN"
During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group. The current validation group is determined by the control that caused validation to occur. For example, if a button control with a validation group of LoginForm is clicked, then the IsValid property will return true if all validation controls whose ValidationGroup property is set to LoginForm are valid. Other controls such as a DropDownList control can also trigger validation if the control's CausesValidation property is set to true (and the AutoPostBack property is set to true.) "
The full documentation of validation group are here: http://msdn.microsoft.com/en-us/library/ms227424.aspx
Bottom line, being...if you want everything validated, no need to use validation groups.
Regards,
- Joel
"The truth is rarely pure and never simple."
- Oscar Wilde, The Importance of Being Earnest
![]() |
0 |
![]() |
langjoel,
1) I had removed the OnServerValidate attributes in order to test if my client-side validation was setup properly and working. I found that it wasn't, and it was because of my typo. I have added it back in so that server calidation occurs.
2) I normally didn't have ControlToValidate, but wanted to include that in my description in case it mattered as far as the CustomValidator control performed differently if set or not (as I don't know specifics about validators).
3) I normally don't use ValidatorGroups, this falls into the same category as the previous -- I had tested it out to see if it would change my results.
End conclusion - I had a typo (should be IsValid and not isValid) which caused my client-side validation to not perform. I wil do some testing and see if I can come back with results of how client and server validations work together.
![]() |
0 |
![]() |
Chris,
Going back to your original question, you should only get a postback if all validators are valid (or all in teh group if using groups). I thought the post I referred to included a link to http://msdn.microsoft.com/en-us/library/aa479045.aspx, which states:
When the user pushes a button that has the CausesValidation property set to True, the validators are all re-evaluated. If they are all valid, then the form is posted to the server. If there are one or more errors, a number of things happen:
- The submit is cancelled. The form does not get posted back to the server.
- Any invalid validators become visible.
- If there is a validation summary with ShowSummary=true, it will collect all the errors from the validation controls and update its contents with them.
- If there is a validation summary with ShowMessageBox=true, it will collect the errors and display them in client message box.
Aaron
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
![]() |
0 |
![]() |
Aaron,
Thanks again for clearing some of this up for me, and shame on me for not having followed through with that link before. This is starting make sense. I just did a test with two controls with validations. One control had client-side and server-side, the other control only had server-side. When both had invalid entries, only the client-side error showed. Once the client-side validation control was corrected, the second control with only server-side displayed an error. Now this works as I would expect. Thanks for your help Aaron.
Chris
![]() |
0 |
![]() |