3

Is it possible to fire a JavaScript method after a form element is considered invalid? Here is my scenario:

There are 2 tabs on the ASPX page. The user has to fill out info on both tabs. The user, while on tab 2 clicks the submit button. However, there is a required field on tab one that needs attention. Do I need to create a custom valuator (either a CustomValidator control or create a new control from the base valuator) to call a JavaScript function to display tab 1 and show where the error is?

DDiVita
  • 4,225
  • 5
  • 63
  • 117

2 Answers2

1

Unfortunately, the canned Field Validator controls in ASP.NET Webforms are not very extensible. I've had needs to change the CSS class of an input field to an invalid state upon client-side validation, and I never found a good way to handle this.

I think your best bet might be to do your own client-side validation. I've also looked into this third party product and it seemed to be pretty fully-functional, but I couldn't justify the cost in my case: http://www.peterblum.com/DES/Home.aspx

Mike Cole
  • 14,474
  • 28
  • 114
  • 194
0

You can call any js function from your server side code after the validation check on page object and inside the js function you can write the logic to highlight the field which has issue on validation:

if(!Page.IsValid)
    {

      Page.ClientScript.RegisterStartupScript(typeof(Page), "validate", "myjsfunction();", true);
    }
    else
    {
       // type code here
    }
RHM
  • 351
  • 2
  • 7
  • 20