Click here to Skip to main content
11,438,007 members (46,269 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 6-Nov-10 20:41pm
ptvce1.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

UpdateButton.OnClientClick = "return validateFun();"
// Call following function in client click event of Update Button
function validateFun() {
var rf=$get('<%= RequiredFieldValidator1.ClientID %>')
;
                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;
}
  Permalink  
v2
Comments
pradiprenushe at 18-Jul-12 6:17am
   
good solution.
Wes Aday at 18-Jul-12 7:56am
   
Please do not resurrect ancient threads.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You may want to try
CausesValidation="true"
on your button, UpdateButton.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,

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

OnclientClick="return check();"


<script type="text\javascript">
function check()
{
if(Page_ClientValidate()){
return confirm("Save the Changes?");
}
else{
return false;
}
}


</script>
  Permalink  
Comments
CHill60 at 2-Nov-14 13:10pm
   
Yep it's late and doesn't actually answer the question asked
Manas Bhardwaj at 2-Nov-14 13:42pm
   
Yup, right :)
Manas Bhardwaj at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.150506.1 | Last Updated 2 Nov 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100