views:

28

answers:

3

In ASP.Net, how do I limit validation messages to only one?

Rule: User must put in a number between 0 and 1000.

Current [undesired] Behavior:

User types 1001: validator says "Please put in a non-negative number beneath 1000"
User types -1: validator says "Please put in a non-negative number beneath 1000"

Desired Behavior:

User types 1001: validator says "Please put in a number beneath 1000"
User types -1: validator says "Please put in a non-negative number"

In other words, how can I use two asp:RangeValidators whose disallowed values intersect but only turn on the desired one? I do not want to handle a ServerValidate event on the server.

Is it doable without client-side code?

+1  A: 

Your 'desired' behaviour has the potential to be somewhat irritating:

  • User enters 1001
  • App tells user "Please put in a number beneath 1000"
  • Fair enough, user thinks, and enters -1 (which you will agree is "a number beneath 1000")
  • App then tells user "Please put in a non-negative number"
  • Why didn't you mention that to start with!?, user thinks to themselves

What I'm asking is: What's wrong with the existing behaviour, whereby the user is told the actual rule?

AakashM
Let's say user types in "AAA"?I wish it to say "Please enter a number".<br/><br/> Right now it says (after implementing an asp:CompareValidator):<br/><br/> Price should be numeric<br/>Price should be non-negative<br/>Enter a smaller number<br/><br/>because technically it fails three validations but only the top one has any relevance.
JonathanWolfson
+1  A: 

You can use a CompareValidator to checks whether the value is less than zero and a RangeValidator to check whether the value is between -2147483647 and 1000.

<asp:CompareValidator Id="IsNotNegativeValidator"
                      ControlToValidate="TextBox1"
                      ValueToCompare="-1"
                      Operator="GreaterThan"
                      Type="Integer"
                      Text="Please put in a non-negative number"
                      runat="server"/>

<asp:RangeValidator   Id="IsLessThanOneThousandValidator"
                      ControlToValidate="TextBox1"
                      MinimumValue="-2147483647"
                      MaximumValue="1000"
                      Type="Integer"
                      Text="Please put in a number beneath 1000"
                      runat="server"/>

(In this particular case, it does seem like a single RangeValidator with the message "Please enter a number between 0 and 1000" would result in less user frustration.)

Jeff Sternal
Thanks- I thought asp:CompareValidator is used only to compare controls, I never knew of the ValueToCompare attribute.
JonathanWolfson
Let's say the user enters a non-integer, thus triggering these two irrelevant messages in addition to a third validation message specifying that the user must enter an integer.
JonathanWolfson
Good question about non-integers - I don't think there's a way to do it without a custom validator or regular expression validator. That's yet another good reason to create a single validator with one comprehensive message stating *all* the rules governing the value. I can't imagine a user having a hard time figuring out which part of "Please enter a number between 0 and 1000" they violated.
Jeff Sternal
A: 

The best way I've found to do this is to use the RegularExpressionValidator exclusively. You can validate anything with the appropriate regular expression.

Jamie Ide