Can I tie a validator to two controls at once?

A

Alan Silver

Hello,

I have two dropdown controls on a form, representing a month/year
combination. The date entered must be in the future, so I wrote a custom
validator to check this. Trouble is, the validator is only tied to one
control (currently the month dropdown), which means that the validation
isn't fired when the other control's value changes.

Is there a way of getting the client-side validation code to fire when
either one of two controls is changed? TIA
 
A

Alan Silver

These validation controls opened up a whole new world for me:
http://www.peterblum.com/VAM/Home.aspx

You can fulfill your requirement, plus more.

Thanks, they look very good.

I assume from your answer that what I want to do can't easily be done in
ASP.NET? I wondered if the changes in 2.0 had brought any improvements
to the validation controls.

Thanks for the reply
 
J

Jay Douglas

From what I've noticed, the validation controls in 2.0 have not made great
gains in the way of functionality. There are some new things like setting
focus on error and a few other items.
 
A

Alan Silver

From what I've noticed, the validation controls in 2.0 have not made great
gains in the way of functionality. There are some new things like setting
focus on error and a few other items.

There was talk of validation groups, but I haven't had time to look in
the docs and see if that made it in.
 
J

Jason Kester

You can do anything you want. You're a programmer!

This is pretty straightforward. I just add another attribute to the
validator tag, like secondControl="txtDate2". It will render as an
attribute of the DIV used for the validator and you'll get a reference
to that DIV (or is it a Span?) in the custom validation function. Just
grab the attribute, getElementByID(), and you're good to go.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

You can do anything you want. You're a programmer!

Oh yeah, I forgot!!
This is pretty straightforward. I just add another attribute to the
validator tag, like secondControl="txtDate2". It will render as an
attribute of the DIV used for the validator and you'll get a reference
to that DIV (or is it a Span?) in the custom validation function. Just
grab the attribute, getElementByID(), and you're good to go.

OK, but the one bit I don't get is how I get the validation routine to
be fired by the second control. You'll have to excuse me, but my
knowledge of the client-side bits is pretty sketchy at the moment.

If I read your words right, adding the attribute merely gives me a
reference to the second control. I can already do that from plain old
Javascript, that's how I do the validation now. What I need is to get
the second control to fire the validation code.

Maybe I'm missing something here, but I'm not sure how your suggestion
helps. Thanks for the reply, any further ideas would be welcome.
 
J

Jason Kester

In practice, I've always found that these two-control validators really
key off a single control. For a date range, for example, you're not
going to want to stick up a red warning message before they even get
around to filling in the second box.

But if you really want to fire for each control, it's simple enough.
View source, copy the onchange="..." out of the control that's hooked
up through the validator. Paste it into the unhooked input control,
tweak it to fit your needs, and you're good to go.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

In practice, I've always found that these two-control validators really key
off a single control. For a date range, for example, you're not going to
want to stick up a red warning message before they even get around to
filling in the second box.

No, I see your point. But there are conditions in which this approach is
useful.
But if you really want to fire for each control, it's simple enough. View
source, copy the onchange="..." out of the control that's hooked up
through the validator. Paste it into the unhooked input control, tweak it
to fit your needs, and you're good to go.

Except that the code doesn't seem to hook up to the onchange event.
Truth is, I can't actually see how the client-side validators fire at
all from looking at the HTML produced.

For example, here is the code for the <select> for this control...

<select name="drpFromYear" id="drpFromYear">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>

</select>

Nothing there to tell the browser to fire the event when the user
changes the control. I can't see anything relevant elsewhere in the file
either.

Any idea? Thanks
 
J

Jason Kester

Nothing to stop you firing it manually. It's just javascript after
all.

<select name="drpFromYear" id="drpFromYear"
onchange="runYourValidator()">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>


</select>

<script>

function runYourValidator()
{
// validate by hand and flip the validator divs on and off as
needed.
}

</script>


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

Nothing to stop you firing it manually. It's just javascript after

OK, that's certainly a help, but it would be nice if there was a way to
fire the proper validation code. I presume that there must be an
onchange event buried deep in the validation code that the framework
provides. Why can't I just get that to fire? Or put a better way, why
can't I call something in the onchange event of my dropdown that will
fire the validation code with the appropriate objects passed as
parameters?

One big advantage of the built-in way of doing it (as opposed to what
you showed) is that you get the chance to set IsValid, which
automatically sets the display of the validation summary, sets the error
message next to the control (using the ErrorMessage property of the
individual validator), pops up the message box and cancels the form
submit. That's a lot of work done for you. Sure I could do this all
myself, but it's a lot of work and is not guaranteed to work in the
future. MS could easily change the way the validation works, and my page
would stop working.

Thanks for the reply. Any further ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top