Enforcing that value entered is greater than a specified value

B

BobRoyAce

Let's say I have a text box on a page in which a user is to enter a monetary
amount and that I want to ensure that the user enters a value greater than
or equal to a certain value that will be determined by interrogating a
database at runtime. There is a button on the page that the user clicks on
to Save the data they have entered. In the click event of this button I want
to check to see if they have entered a value no greater than the maximum
value allowed. What is best way to do this? I have tried using a
RangeValidator (of type currency), specifying its maximum value, but that
control gives strange behavior. For example, if maximum value specified is
45, and the user enters "45.", the control reports back that the value is
not acceptable. .
 
K

Karl Seguin

Bob:
I think the range validator is the right way to go. I'm not sure I agree
with you that what you describe is a strange behaviour. You are telling it
that the type is 'currency' and 45. simply isn't a valid currency. If you
enter 45, 45.00 or 45.0 it'll work, but I think it's right in complaining to
the user that 45. simply isn't valid.

Karl
 
P

Peter Blum

The RangeValidator isn't the ideal validator. It can be used but Microsoft
intended the CompareValidator to handle this.
You also say "maximum" and "greater than or equal". These are not
consistent. If you want >=, you are looking for a minimum.

Here's how to set it up (I'll assume a minimum):

Type="Currency" (often this is forgotten, and leaving it as "string" causes
all kinds of matching problems)
ValueToCompare=[your minimum]
Operator="LessThanEqual"
ControlToValidate=[TextBoxID]

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

BobRoyAce

Well, then why doesn't 45.000 work?

Karl Seguin said:
Bob:
I think the range validator is the right way to go. I'm not sure I agree
with you that what you describe is a strange behaviour. You are telling
it
that the type is 'currency' and 45. simply isn't a valid currency. If you
enter 45, 45.00 or 45.0 it'll work, but I think it's right in complaining
to
the user that 45. simply isn't valid.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
B

BobRoyAce

You're right...should have said less than or equal to...oops.

I am thinking that CompareValidator (at least not a single one) is not right
because actually I also want to make sure that bid is greater than zero
(0)...thus the range.

Bottom line is that I want to make sure that users enter values greater than
zero and less than or equal to a specified value.
 
B

BobRoyAce

Even using the CompareValidator, with the settings you specified, I get the
same results. If I set a ValueToCompare value equal to 10, and then enter
"10." it flags the value as not meeting the criteria. "10.000" does the same
thing. So it seems to be an issue not with the control so much as with its
being a Type of "Currency". Any ideas?
 
B

BobRoyAce

Just a little update...even if I change the Type to "Double", I still get
the condition of not meeting criteria if I enter a value of "10."...so
appears to not just be an issue with "Currency" type.
 
P

Peter Blum

I'm not seeing the problem right off the bat. I agree that you should use
RangeValidator since you do have a range.

Try this. Add a CompareValidator with Type=Currency and
Operator=DataTypeCheck. (You really should have this anyway. It confirms
that the text pattern is a valid Currency. The RangeValidator will only
attempt to evaluate your entry if the number is a currency. It will never
show an error for invalid text formats.)

Now you should be able to determine if the validator simply doesn't
understand the format of the value. If it does not, I'm wondering if your
CultureInfo settings on the current thread have a different character for
the decimal separator. You are typing a period. Maybe its using a comma
(which is common in some cultures). The Range and CompareValidator uses the
CultureInfo object on the current thread to get formatting information for
dates and numbers. You can control it by reading up on the CultureInfo
object and setting the desired culture either on the thread object or in <@
Page culture="[culture]" >.

--- 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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top