views:

2029

answers:

2

Hello,

I have an issue that's been bugging me this morning. I'm building an ASP.NET webforms app that has many input forms and I'm trying to standardise how I manage validation. I would like to use the built-in validators (RequiredFieldValidator, Regex etc). My html requirements are:

Before validation:

<div class="formLine">
   <label for="fieldID">Form Label</label>
   <input type="text" id="fieldID" />
</div>

After validation (in case of error):

<div class="formLine formError">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
<span class="errorMessage">Please enter some value</span>
</div>

The additional span is fine, this is achieved with the ASP.NET validation controls. My problem is adding the formError class to the containing <div>.

I'm comfortable using jQuery/DHTML to add this class, but just don't know where to hook it in. Is it possible to monitor DOM changes with jQuery - for example fire an event handler whenever a span is added as a child of <div class="formLine">?

Does anyone else have any good suggestions for dealing with this?

Thanks for reading.

A: 

EDIT: After clarification

You could write a custom function that is called by the onClick client side event. Because you are using ASP.Net validation controls you should be able to call the function ValidatorValidate(val) where val is the validator. You could then check the contents of the validators SPAN tag for your validation error and update your div tag accordingly.

Note: I'm no expert on client side validation but this should work in principal.

Further info on MSDN.


You need to declare your div as runat="server" with an id tag so you can reference it in server side code then in page load do a test for Page.IsValid which will return false if one of your validators fails to vaildate.

You can then set the class property on your div programatically. Something like:

<div class="formLine" id="divForm" runat="server">   
  <label for="fieldID">Form Label</label>   
  <input type="text" id="fieldID" />
</div>

Code-Behind (VB)

If Not Page.IsValid Then
  divForm.Attributes.Remove("class")
  divForm.Attributes.Add("class", "formLine formError")
End If
Charlie
Hi Charles. Thanks for reply. I was thinking of doing this, but I would like to also like to have client-side validation. I also don't think you can call Page.IsValid in Page_Load (just event handlers for controls or after calling Validate()).
Andrew Corkery
You can call Page.IsVlaid in this context. You might additionally call your validators Validate method and IsValid property respectively.
Charlie
Spelling above: IsValid not IsVlaid (how about allowing comment editing guys!)
Charlie
Note you can have client side validation too, but the server side validation would only handle this aspect because the client side validation would fire first.
Charlie
When I have IsValid inside my Page_Load it throws the following runtime error:"Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate."
Andrew Corkery
Yeah I second comment editing! You're approach above would definitely work for post-backs, just wondering how I might achieve the same effect on client-side validation.
Andrew Corkery
RE: page load comment. I should have said you would need to wrap it in an If Page.IsPostback() otherwise it will try to validate the page on initial load... Sorry!
Charlie
Hey charles, no problem. I did try that, but according to the docs (http://msdn.microsoft.com/en-us/library/ms178472.aspx). the Validate event occurs after Load. Anyway, I can do this in PostBack event handling stage. Bloody webforms...
Andrew Corkery
Have you checked out my edited answer above?
Charlie
Yep. Thanks for that, I think I'll definitely have to look more closely at the ASP.NET JS code. I'd also like to have functions that update the display when the user tabs between fields, so I guess this is the place to look.
Andrew Corkery
A: 

i'm assuming you're looking for a way to do this on the client side, without postback.

when you use asp.net validation controls on your webform you'll notice that a script reference to WebUIValidation.js is rendered on your page. peeking into that file offers some useful info. there's a global variable in that file named Page_IsValid. whenever validation fails, that variable is set to false.

EDIT: the WebUIValidation.js script reference was prior to .net 2.0, so you can't look at that file anymore. however, the Page_IsValid variable is still there and can be used by hooking into a form submit handler. something like this

// verified that this works
$(document).ready(function() {
    $('#form1').bind('submit', function(e) {
        if (!Page_IsValid)
            $('#yourDiv').addClass('error');
    });
});
ob
Thanks for the info, I've never used that variable, that would do the trick however at the button "onclick" stage the value would always be true (client validation happens after this?) and the "onsubmit" action is stopped by client validation, so I can't hook in there.
Andrew Corkery