How use javascript with asp.net forms?

C

COHENMARVIN

I have a problem that sounds simple but seems impossible with asp.net.
I calculate a variable in the Page_Load event, call it X. Then I want
my range validators to fire when a textbox value is greater than X.
They should give an error message, both as a messagebox and inline in
the page. I can't get the range validators to use X. I get syntax
errors when I try. I tried Custom Validators and got the same problem.

So now I want to try using client side javascript with forms. This may
not work either, because I calculate X in page_load, and I don't know
how to communicate X to the javascript. But forget that for now, does
anybody know how to intercept the SUBMIT click of an asp.net form and
use client side javascript validation on it?
 
A

Alex D.

pass the X in a hidden field, but use Styles to hide the field, otherwise
it wont pass any value. in javascript I guess you can use the events fired
when moving away from the textbox and compare that to the hidden field.
 
O

Oliver Wong

Your post is a bit cluttered, with 3 problems embedded in it. I'll try
to handle them one by one.

COHENMARVIN said:
I have a problem that sounds simple but seems impossible with asp.net.
I calculate a variable in the Page_Load event, call it X. Then I want
my range validators to fire when a textbox value is greater than X.
They should give an error message, both as a messagebox and inline in
the page. I can't get the range validators to use X. I get syntax
errors when I try. I tried Custom Validators and got the same problem.

Syntax error means you've typed in something which does not represent a
valud ASP.NET program. If you posted some source code, someone might be able
to find the syntax error and help you out with that.

So now I want to try using client side javascript with forms. This may
not work either, because I calculate X in page_load, and I don't know
how to communicate X to the javascript.

You could output X as a hidden form input, e.g.:

But forget that for now, does
anybody know how to intercept the SUBMIT click of an asp.net form and
use client side javascript validation on it?

I don't know of a clean way of doing this. It's possible, but would
require lots of messy hacks, so you'd probably be better off with tackling
one of the above two problems first.

- Oliver
 
P

Peter Blum

I agree with Oliver, who suggests continuing to try to use the validators.
There is no reason why you cannot get a CustomValidator to handle what you
want.

When you state you are calculating a value on the server side and using it
with a RangeValidator, are you using that value as the Minimum or Maximum of
the range? If so, there is no javascript involved. You convert the number to
a string and assign it to one of those properties. The RangeValidator
handles it from there. Just make sure RangeValidator.Type is Integer,
Decimal, or Currency.

Actually I notice that you want the textbox value greater than X. So lets
switch from RangeValidator to CompareValidator. Set the Operator to
GreaterThan and ValueToCompare to the string of the number you calculate.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top