Click here to Skip to main content
15,884,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a RequiredFieldValidator applied to a TextBox.
At the same time, I set the submit button's OnClientClick property as follows:
UpdateButton.OnClientClick = "return confirm('Save the changes?');"

when i set onclientclick then my validator not work
Posted

I did the same. And it was working fine.You must be doing something wrong.

Actually, when you set the OnClientClick property, then this code will be executed first, then if postback occurs the validation will be fired.

In your case, On clicking of UpdateButton, it will show the confirmation box, if user clicks OK then validation will be fired because page will try for postback and if clicked Cancel, then nothing will happen and validation will also not be fired.
 
Share this answer
 
UpdateButton.OnClientClick = "return validateFun();"
// Call following function in client click event of Update Button
function validateFun() {
var rf=$get('<%= RequiredFieldValidator1.ClientID %>')
C#
;
                if (rf != null){
            ValidatorEnable(rf, true);
                    ValidatorValidate(rf);
                    if(rf.isvalid==true){
            var answer=window.confirm('Save the changes?');
            return answer;
                      }
                     else
                     return false;
            }
         else
         return false;
}
 
Share this answer
 
v2
Comments
pradiprenushe 18-Jul-12 6:17am    
good solution.
[no name] 18-Jul-12 7:56am    
Please do not resurrect ancient threads.
You may want to try
C#
CausesValidation="true"
on your button, UpdateButton.
 
Share this answer
 
Hi You need to use Page_ClientValidate() function to invoke the page validation.
The below links will give you a more information.

http://stackoverflow.com/questions/8918722/onclientclick-code-preventing-validation-from-working[^]

http://ideaofinnovation.blogspot.in/2012/03/validation-control-not-working-after.html[^]
 
Share this answer
 
Hi,

I know its late but hope this may be helpful some one who just googled :).

OnclientClick="return check();"



function check()
{
if(Page_ClientValidate()){
return confirm("Save the Changes?");
}
else{
return false;
}
}


 
Share this answer
 
Comments
CHill60 2-Nov-14 13:10pm    
Yep it's late and doesn't actually answer the question asked
Manas Bhardwaj 2-Nov-14 13:42pm    
Yup, right :)
Manas Bhardwaj 2-Nov-14 13:41pm    
Why opening an old thread?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900