How to validate a number in asp.net? - c#

I have a TextBox that the user need to fill with data a number between 0-90
with 3 decimal places (ex. 0.123 , 1.456 , 1.3 , 45)
i need to validate it.
i can use some validation controls.
RangeValidator with type double, min 0 , max 90
RegularExpressionValidator
what is better?

Do it with progressive enhancement in mind.
First make a non-javascript version where the textbox is validated server-side.
Second make a nice javascript validation using a RangeValidator.
Keep the RegEx validator's for phone numbers (1800 Plumbing) , emails, fax's & etc.

Is this 3 decimal places exactly or it can vary? If it’s 3 exactly I’d go with regex validator because I’m not sure range validator can validate for exact number of decimal places.
Also you need to make sure these are validated on the server side as well.

Related

How to properly check is real number mask in maskedtextbox completed or not?

Let's say there is a MaskedTextBox which accepts real numbers in 0.00 - 99.99 range. At least one digit in integral part is required, digits in decimal part are optional.
So I can set Mask to 90.99 and after that rely on MaskedTextBox.MaskCompleted property. But if user enters for example 1_.00 then MaskedTextBox.MaskCompleted=false.
I can set Mask to 09.99, but again if user enters for example _1.00 then MaskedTextBox.MaskCompleted=false.
So is there a way to set up mask to have MaskedTextBox.MaskCompleted working properly.
Or the only way is directly reading and analyzing MaskedTextBox.Text in my code?

Textbox that accepts upto 4 digit decimals

I'm looking to accept digits and the decimal point for forms, using WPF application.
I am new to WPF
I want a textbox that takes only numeric values, in the format
12.3456
that is, 2 places before decimal point and 4 places after it.
Too bad TextBox has no Property to set the DataType of the field and the amount of Decimal Places it can hold, had to make a UserControl that does exactly what you want.
Offtopic aside, use Regex, I usually use this website to build them.

In Unity3D, remove all but a set amount of decimals from a number

For a text display in Unity3D. Wht would I do if I wanted to remove all but say 2 decimals from a float? To clarify what I want to do, please see the example below:
Say I have the floats:
1.25013
1.9012
1.029
Now, in Unity what should I do if I want to shorten them so only two decimals remain i.e returning 1.25, 1.90 and 1.02 before displaying the string such as on a UI.Text ?
I have looked into string formatting and stuff like Math.Floor without success.
As said in the comments, if it is just for display the correct way to solve this problem is pass in a numeric format to a .ToString call.
To get the format you described you would want the format string f2, that would create a fixed-point number with two decimal points that will look like 1234.50
UI.Text = someNumber.ToString("f2");
If you would like a , in the number when you have more than 3 digits to the left of the decimal place like 1,234.50 use n2 instead of f2
UI.Text = someNumber.ToString("n2");

Regular Expression with specific required numbers on both sides of decimal point

I am trying to figure out a regular expression for an asp.net RequiredFieldValidator that validates decimal values from 1.001 to 99.9999.
This means every value greater than or equal to 1.001 and less than or equal to 99.9999.
The closest I have managed is:
(?=\d+(?:\.\d+)?$)(?![0\.]+$).{1,7}$
This still allows 0.1 and 1.0001, how do I prevent these values?
You can use negative lookaheads for that task:
^0*(?!1(?:\.0+)?$)(?!1\.000)(?!99\.9999.*[1-9])[1-9][0-9]?(?:\.[0-9]+)?$
Demo
I don't know asp.net, but maybe you could use something like this?
[1-9][0-9]?\.[0-9]{2}[1-9][0-9]?
However, maybe converting this to float and checking its inside the interval would do the trick, without the use of regexp...

Calculating the Maximum Value for International Currencies - less one unit

I am attempting to validate a range dollar amount. The value must be less than (not equal to) the input currency amount, which can be represented in multiple currencies.
How can I represent this value? Should I convert to decimal and subtract 0.01? If that doesn't work, why not? I'm using C#.
I'm possibly over-thinking an obvious solution, so an "Uhhhh do 'X' " type of response would not surprise me.
Thanks for any insight.
Seeing as how you say it can be in different currencies, Are the values you are comparing both of the same currency? If not then your range will need to be even more dynamic as some foreign currencies may result in a larger number than your initial value.
So for example if you are comparing a USD 100 to YEN which would be about 92 yen per dollar for 9200 yen (on average) for today. that would not really match up. It's best to compare same currency to same currency for ranges.
Also you never said what your range is. You are only talking about your Maximum. What's your min? Is it Zero what if you have a negative amount? If this was a trading application you could have positive and negatives.
Your best bet if you have a maximum value is to convert your entered value to whatever similar currency you have and then adjust validate that it's less than or equal to your max value. Which might be better in a codebehind validation vs a range validator.
So lets assume you are using a trading app. You have a Trade Limit say 1000 USD. The user is doing an international trade in YEN. When you go to do your checks you will have to get the FX Rate for the day, divide it into your YEN entered amount and verify that it's less than or equal to your limit. In a trading scenario you could use an ABS value so that a Buy or Sell net out to the same 1000 limit. Since this is a bit more complicated than a simple range check I would personally recommend doing your own validations either with jQuery / AJAX or just plain code behind.
May make your life much easier.

Categories

Resources