only two digits allowed after decimal asp.net - asp.net

I have a textbox where the user must not be able to enter more than two digits after a decimal.How do I do this without using javascript?
Thanks.

You can set the MaxLength property of the textbox, but that doesn't have any notion of whether or where the decimal point is.
You could also use a CustomValidator and check the inputted number on the server via the ServerValidate event. But this will require going to the server to check the value (i.e. it will initially look like your form allows users to input invalid numbers).
You should also be able use to a RegularExpressionValidator, depending on your exact globalization requirements, which will use JavaScript on the client to provide immediate feedback:
<asp:TextBox ID="NumberTextBox" runat="server" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="NumberTextBox"
ValidationExpression="\d+(?:(?:\.|,)\d{1,2})?" />
If you want the immediate feedback to the user, you'll need to use a JavaScript based solution.

You cannot cause the textbox to stop accepting text after two decimal places without directly or indirectly using javascript. (This is sometimes called an input mask).
You can, however, allow the user to enter free-form text and validate the text upon postback on the server. You can either automatically round the number for them, or return an error message to the client.

If you really need to prevent the user from entering more than two digits after the decimal point, you'll need to use JavaScript or a server control that implements the JavaScript for you.
However, may make more sense to allow them to enter any number of digits and then catch it on validation (or just round to two digits).

Related

RegularExpressionValidator to limit input length and allow empty strings

I'm really bad with regex and was looking at another question almost identical to this but can't get it to work as I need.
I was to use a RegularExpressionValidator that will allow any character up to 255 characters or nothing at all. I tried,
ValidateExpression="^.{255}$"
but it throws an unhanded exception when the textbox that I'm checking is empty.
I've also tried
ValidateExpression="^.{,255}$"
Thank you
Rodney
Did you try ^.{0,255}$? Not sure what exception you are getting though.
EDIT: Also, if struggling with .Net regex, Regex Hero tester is a great help. I know there are other sites, but this one is by far the best, if you ask me.
The proper expression is ^.{0,255}$.
^.{255}$ will only match if there is exactly 255 characters, while ^.{,255}$ will match the literal string "{,255}".
If there are still issues after trying that, can you tell us the exception?
If it throws an HttpRequestValidationException exception, you can use the members of the UnvalidatedRequestValues class.
Mind that "Validation succeeds if the input control is empty" (MSDN). You may add a RequiredFieldValidator field to ensure that the user does not leave a text box blank (if you use the RequiredFieldValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel - MSDN):
<asp:textbox id="myTB"
runat="Server">
</asp:textbox>
// ... MAKE SURE YOU DO NOT USE TextMode="Number"!!!
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="myTB"
ErrorMessage="Please enter a number.<br />"
Display="Dynamic"
runat="server"/>
And as for regex, ^.{255}$ means match any character (except newline) exactly 255 times between string start and end. I think it makes sense to allow ^[1-9][0-9]{0,254}$ (for values like '34', '104', etc.).

disable characters from being entered in textbox

I am building a form and i want to prevent certain characters from being typed in a textbox?
I want to disable the " and ' characters in some of my textboxes
You can use a RegularExpressionValidator along with a regex such as [^"'] to allow all characters except " and '.
(please note, that regex is untested at the moment...)
ASP.NET Validators have both client and server APIs for validation.
The safest way is to check the form values on the server-side to see if the input is valid (doesn't include quote characters in your case) and respond with an error if it is invalid.
On the client side, I've had good luck using jQuery Validation.
Remember that arbitrary form payloads can be constructed outside of a browser, so you always have to check data validity on the server, even if client-side validation is in place.
Using an AJAX ASP FilterTextBox Control would be the way I'd go.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="TARGETCONTROLIDHERE"
FilterType="Custom, Numbers, LowerCaseLetters, UpperCaseLetters, Symbols"
InvalidChars="&" />
You can simply remove those characters from the string before you perform your database operation.

RangeValidator Problem in ASP.NET

I was using RangeValidator to validate user input on client side for double values.
One of my user said that when he enters 5E-10, my range validator does not understand that number as a valid double.
What do you suggest me to do?
Thanks,
cas
This is a known problem with RangeValidator.
You'll need to use a CustomValidator instead, and manually verify the user's input is within the allowed range - including allowing scientific notation.

ASP.net MaskedEdit: Entering number 425.25 produces 4250000000.25. How can I fix this?

I had a NUMBER field in an ORACLE Database that is set to 13,2. I want to use the MaskedEdit field in order to mask this.
If I enter 425.25, it produces a 4250000000000.25, instead of moving the 425 over. I'm literally entering 425 pressing the period key and then 25, which moves me into the decimal area of the MaskedEdit. That works great, but I need the main integer to move down and not fill with zeros.
Any ideas?
It could be a localized version of your software that is misinterpreting the character . (period). Please try a , (comma) in substitution to the period.
Additionally you should check the CultureInfo, which is an important tool to prevent errors like this, you should set it on your Web.Config and every method that accepts it, like MS Code Analysis tells you so. Common methods that accepts a CultureInfo parameter are:
Parse (int.Parse, double.Parse, ...)
ToString (int.ToString, double.ToString, ...)
Check your Mask property:
This is not the correct markup, just an example from Ajax Control Toolkit site.
<ajaxToolkit:MaskedEditExtender
TargetControlID="TextBox2"
Mask="9,999,999.99"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Number"
InputDirection="RightToLeft"
AcceptNegative="Left"
DisplayMoney="Left"
ErrorTooltipEnabled="True"/>
Dropped the MaskedEdit control and used a JQuery plugin to produce the same effect, only not flawed.

ASP.Net Validation

I want to validate the value a user enters in a text box, so that it only enters float numbers. I'm not interested in range. How can I do this, considering also culture localization information (e.g. "." or "," as separators)?
My usual method is to use a RegexValidator with a validation expression of ^(\d+(\.\d*)?)|(\d*(\.\d+))$. You could ammend this to enable "." or ",": ^(\d+([\.,]\d*)?)|(\d*([\.,]\d+))$.
If you wanted to be strictly correct, you'd enable the correct validation expression for each culture.
Also note that you still need a RequiredFieldValidator if the value is compulsary.

Resources