How can I prevent users from entering anything other than a numeric value or a decimal value with 1 decimal place?
The user should be allowed to enter any length of characters (if decimal value, before the decimal).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try using
Regex. This pattern should work:Regex match = new Regex(@"^[1-9]\d*(\.\d{1})?$"), put that in your validating event of the textbox. If its no match,Undo()or delete the Textbox.Text property.To actually undo the input immediatly, you have to use
Because if you use KeyDown, the TextBox has no Undo State.
2nd Edit: If you want both cases to match, you have to do the check in the Validating Event or a similar one. Since the regex uses “$” to make sure, no characters are added in the end, you cannot enter “.” or else you’d end up having a number like 1. which would require additional checking.