ASP.NET RangeValidator can't do even the most basic math? - asp.net

I'm having an issue with my ASP.NET RangeValidator controls.
I want to allow users to enter a discount amount, and this amount must be negative (< $0.00). I want to verify that the amount entered in a textbox is a negative value, so I have this in my page markup:
<asp:TextBox ID="tbxDiscount" runat="server" />
<asp:RangeValidator ID="rvDiscount" runat="server" ControlToValidate="tbxDiscount"
MinimumValue="0.0" MaximumValue="0.0" EnableClientScript="true"
ErrorMessage="Please enter a negative value for a discount" />
and I attempt to set the MinimumValue dynamically in my code before the page gets rendered - to the negative equivalent of my item price. So if the item is $69, I want to set the minimum value to - $69:
rvDiscount.MinimumValue = (-1.0m * Price).ToString();
Trouble is: I keep getting this error message:
The maximum value 0.0 cannot be less
than the minimum value -69.00 for
rvDiscount
WTF?!?!??! Where I come from, -69 $ IS less than $0 ...... so what's the problem?
And more importantly: what is the solution to the problem??

It's not trying to do maths, it's doing a string comparison because you haven't told it otherwise. Try adding this attribute:
Type="Double"

Related

Using ID as a rangevalidator value ASP

I've been working on a small game and I'm stuck trying to figure out how I can convert an ID that a user enters to become an integer value. I'm wanting to create a rangeValidator that specifies that a bet entered by a user can only go from 0 to the total amount of money they currently have.
current money: <asp:label ID="currentMoney" runat="server" Text=""></asp:label> <br />
Place bet: <asp:textbox ID="bet" runat="server"></asp:textbox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ForeColor="Red" ControlToValidate="bet" runat="server" ErrorMessage="bet must be less than or equal to current money"> </asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="bet" type="Integer" MinimumValue="1" MaximumValue="currentMoney" ></asp:RangeValidator>
I'm not sure if there is a way to convert the currentMoney ID which gets originally entered by a user at an earlier part of my code to be used as the maximum value for my range validator. Is there any suggestions anyone might have?
You'd have to set the MaximumValue property in code:
RangeValidator2.MaximumValue = currentMoney.Text
As long as everything happens on the server. If not, then you'd have to update the client API; if you do that, when a postback happens, you'd have to update on the server or update on the client again (as these are not sent back to the server). I'm not quite sure how to do that, but googling should provide a solution.

RangeValidator currency validation makes no sense

I'm having some issues using a RangeValidator in ASP.NET 4.5. The issue I'm facing is trying to validate a range of currency values.
Let's say I have the following RangeValidator:
<asp:RangeValidator ID="_rngValCustomAmount" runat="server"
CssClass="error-message" ErrorMessage="Please enter a valid amount."
ControlToValidate="_txtCustomAmount" MinimumValue="10.00"
MaximumValue="500.00" Type="Currency" />
When I enter a value of '450' the validation is passed, and I can submit the form. However, if I enter '$450' then the validation fails, even though I have set the Type property to Currency.
So, then I thought, OK, maybe I need to enter a currency Minimum and Maximum value, however when I change the control to:
MinimumValue="$10.00"
MaximumValue="$500.00"
I get the following .NET exception:
The value '$500.00' of the MaximumValue property of '_rngValCustomAmount' cannot be converted to type 'Currency'.
I don't really want to have to resort to using a CustomValidator for this, so that I don't have to write a JS function for something that should exist as the RangeValidator supports the currency type. Can anyone point out what I might be doing wrong?

CompareValidator error when value between 3 and 10

I have problem with CompareValidator. I need to validate input1 such that it is less than or equal to "Somepercentage" from database. The control keep showing error message when input1 is between 3-10 even when it is less than "Somepercentage". When input1 is more than 10, it works fine.
<asp:CompareValidator ID="Validator" runat="server" ErrorMessage="CompareValidator" valueToCompare='<%# Eval("Somepercentage")%>' ControlToValidate="input1" Operator="LessThanEqual"></asp:CompareValidator>
I found the solution. add type="Double" into asp:comparevalidator tag.
CompareValidator does not work with single digit

RegularExpressionValidator only checks once

I have a form in which I would like guests to enter their name, address etc...
On each field I have a RequiredFieldValidator and a RegularExpressionValidator.
For example:
<asp:TextBox ID="mailNameTextBox" runat="server" MaxLength="70" ValidationGroup="mail"></asp:TextBox>
<asp:RegularExpressionValidator ID="mailNameTextBox_RegularExpressionValidator" runat="server" ErrorMessage="Name can only have letters or spaces." ControlToValidate="mailNameTextBox" ValidationExpression="[a-zA-Z' ']" ValidationGroup="mail" Display="Dynamic">*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="mailNameTextBox_RequiredFieldValidator" runat="server" ErrorMessage="Name field is required." ControlToValidate="mailNameTextBox" ValidationGroup="mail" Display="Dynamic">*</asp:RequiredFieldValidator>
When I enter a name that includes a number (ie. fails the Regex) and "tab" out of the TextBox, the RegexValidator is instantly displayed with a *, and the form fails to submit. But when I go back and remove the number (ie. pass the Regex), the Validator doesn't seem to re-check the input, and the form will forever fail to submit.
Is there a way to always fire the Validation when leaving the box rather than just the first time (besides calling a Validation method upon the TextBox losing focus). I would have thought this would be automatic?
I have spent some time trying to find an answer to this, and I'm not sure how to word my question to "thouroughly research it", so I'm sorry if this is a duplicate.
I did see this one:
ASP.NET: RegularExpressionValidator Doesn't reCheck the input
but it didn't really help in my case.
Your validation expression is this:
[a-zA-Z' ']
Which means: exactly one letter or one apostroph or one space.
You probably want to use something like the following:
[\w\s]*
Which means: any number of letters or whitespaces.

In asp.net, I need to add validator to textbox that forces the input to be numbers

In asp.net, I need to add a validator to a textbox that forces the input to be numbers.
Is this built in?
I have already added a required field validator to the textbox.
You could use a Regex Validator to ensure the text is numeric
I think the regex would be
[0-9]*
e.g.
<asp:TextBox ID="tbxNumbers" runat="server" />
<asp:RegularExpressionValidator ID="revNumericValidator" runat="server"
ValidationExpression="^[0-9]*$" ControlToValidate="tbxNumbers" ErrorMessage="Must be Numeric" />
EDIT:
As the other two posters also pointed out you can also use \d to represent a Numeric Character
<asp:RegularExpressionValidator runat="server"
ControlToValidate="numbersOnlyTextBox"
ErrorMessage="Enter only numeric characters."
ValidationExpression="^\\d+$" />
Use a range validator.
<asp:TextBox ID="MyTextBox" MaxLength="4" Width="75"
Text="0" runat="server"></asp:TextBox>
<asp:RangeValidator ID="MyRangeValidator" Display="Static" Type="Integer"
MaximumValue="9999" MinimumValue="0" EnableClientScript="true"
ControlToValidate="MyTextBox" runat="server" SetFocusOnError="true"
ErrorMessage="Ooops"></asp:RangeValidator>
This permits you to use numbers with decimal places (by using Type="Double" or "Currency"), or other kinds of numbers that Windows recognizes.
Check MSDN for more info on the Range Validator Control.
I think there needs to be more clarification of the requirements here. What kind of numbers are we talking about? Positive integers? Any integer? A number with a decimal place? What about commas in the number (1,000)?
I recommend a RegularExpressionValidator to do your work, but these questions make a difference when it comes to which RegEx you use.
In order to provide a better user experience, another thing to add is an AjaxToolkit FilteredTextBox extender, with a FilterType of either "Custom, Numbers" or just "Numbers". The first choice is for when you want to be able to specify decimal points and negative numbers. In that case you must also specify the ValidChars attribute with something like "-.". This will stop a user from entering characters that are not going to make up a valid number such as -123.45 . Note that it does not stop the user from entering the '-' & '.' in incorrect places e.g. "2-..-3" can still be entered. You will need the validators mentioned in other answers to catch these cases.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Custom, Numbers"
ValidChars="-." />
Or
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Numbers" />

Resources