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.
Related
How to make asp.net to allow the less than(<) and greater than(>) symbols along with other special symbols to be included in the password field?
I have tried setting the page request validation settings but I can't afford to compromise on security. Still I want the '<' and '>' symbols to be allowed in password fields.
You can only turn off request validation for entire pages; not for single controls. If you want to allow these characters in passwords, you'll have to turn the validation off on the entire page, and take extra care that other input fields are protected against XSS and the like.
Is it possible to put a character sequence in a Filtered TextBox Extender in Asp.NET? My guess is no, but I'm curious. I want the user to be able to enter the characters (such as & and #), but not enter the invalid sequences (such as &#).
Why not just use a regular expression? Because when the form is submitted, all the fields are passed to the server...including fields that are not part of the validation group. These fields do not get checked and may trigger the "A potentially dangerous Request.Form value…”, aka the HttpRequestValidationException. And preventing that message is the whole point of this. I'd rather tell the user, with a regex validator, what they are doing wrong...but I will settle for preventing them from typing the bad chars (&#, <, >).
Edit: sort of an afterthought, but if there's a better way to prevent ALL TextBoxes from including the characters, that'd be great!
You can just have a function onkeydown for all your text boxes and that checks the character pressed and if it's invalid just remove that last character. Also for your combination strings you can just check to see if the previous characters make it invalid then remove all of them.
An example of that type of function is:
function checkKey(event) {
var code = event.keyCode;
// code is the ascii number of the key.
}
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.
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).
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.