Connecting Tech Pros Worldwide Forums | Help | Site Map

custom validator causing postback after error message is displayed

cameokid's Avatar
Newbie
 
Join Date: Feb 2008
Posts: 10
#1: Feb 10 '09
Hi,

I have a table with some form fields. This table is hidden on load. I display it on click of a button.

After i enter values and click button inside this table. It displays the custom validation error inside table (I want the error msg to be displayed inside table) but causes postback and hides the table. How do I stop the postback and display the error message while the table is visible.

The code inside custom validation function is ...
Expand|Select|Wrap|Line Numbers
  1. if (conditon) {
  2. args.IsValid = false;
  3. }
  4. else {
  5. args.IsValid = true;
  6. }

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Feb 11 '09

re: custom validator causing postback after error message is displayed


Set the style of the server in the button click event to display the table if there is an error.

Expand|Select|Wrap|Line Numbers
  1. if (conditon) {
  2.     args.IsValid = false;
  3.  //I'm assuming something didn't validate in this block
  4.  //here you want to set the style of the table to make sure that it's displayed
  5.  //if you aren't using style to hide the table, then make sure that the table
  6.  //is visible in this block.
  7. }
  8. else {
  9.     args.IsValid = true;
Although validating data on the server is always good practice, have you considered using a Client Side Validator Control to help make sure the data is valid before it's sent to the server?
cameokid's Avatar
Newbie
 
Join Date: Feb 2008
Posts: 10
#3: Mar 16 '09

re: custom validator causing postback after error message is displayed


Hey thanks.

Sorry for the late reply.

That is what i had to do. Used "clientValidationFunction" property. Read somewhere on net that all validators do not cause post back except a custom validator. Hence i tried client validation using Javascript to avoid post back and it worked fine. Below is the JS Function.
Expand|Select|Wrap|Line Numbers
  1. function CustomVal_ClientValidate1(source, args)
  2.     {
  3.         var pcdata = document.getElementById("pinCodeTxt").value;
  4.  
  5.         if (pcdata.match(/\d{6}/))
  6.         {
  7.             args.IsValid = true;
  8.         }
  9.         else
  10.         {
  11.             args.IsValid = false;
  12.         }
  13.  
  14.     }
Reply