3

I am having quite a hard time with my C# Application's textbox validation. The thing is, the said textbox should only accept decimal values. so it means, there should be no letters or any other symbols aside from the '.' symbol. The letter filter, i can handle. However, i don't exactly know how I can manage to filter the number of '.' that the textbox should accept. If anybody has any idea how to do this, please give me an idea.

Thank you very much :)

5 Answers 5

4
decimal value;
bool isValid = decimal.TryParse(textBox.Text, out value);

if (!isValid)
{
    throw new ArgumentException("Input must be a decimal value");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Localisable, understandable, +1able
this is what my application was doing before. however, i am avoiding a lot of error messages to pop. so i just want the textbox to be filtered. but thanks still for this :) i appreciate it. hehe
3

this should work!!!

modified for just one decimal

    private void txtType_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Back || (e.KeyChar == (char)'.') && !(sender as TextBox).Text.Contains("."))
        {
            return;
        }
        decimal isNumber = 0;
        e.Handled = !decimal.TryParse(e.KeyChar.ToString(), out isNumber);
    }

11 Comments

this is exactly what I can do now. However, this doesn't filter the number of decimal points which the textbox should accept. it still accepts 2 decimal points. :(
This is exactlyyyyyyyyyyyyyyyy what I needddddddddddddddddddd!! Thanks. now all I need to do is to study your code. :) Thanks a lot. Wait. I can't use the backspace key? hehe.
@Kim careful however, this is not localised - some countries use commas for decimal places.
@Adam, yes I know that but this software i'm working on is just a small exercise for my class. Thanks for reminding :)
Modified it cuz it was for ints not decimals... my bad... but it should work, also you can add some logic to hanlde the minus symbol
|
1

Use regex validation:

^([0-9]*|\d*\.\d{1}?\d*)$

This site has a library of regex validation (including numeric related) that you'll find useful:

http://regexlib.com/Search.aspx?k=decimal&c=-1&m=-1&ps=20

Comments

1

Just a thought: if you are monitoring the decimal places, simply keep a bool flag in your control to say "I've already had a dot"; subsequent dots are invalid.

Alternatively, when checking for decimal places, you can use Contains:

if (textbox.Text.Contains("."))

Also, review this sample available on MSDN (NumericTextBox):

http://msdn.microsoft.com/en-us/library/ms229644(VS.80).aspx

3 Comments

yes, i've seen this and this is actually where I learned how to restrict letters and some symbols in my textbox. However, what i'm looking for is something that would limit the textbox to accept only one decimal point.
+1 for the Numeric Text Box sample. It still needs the "I already have a decimal separator" flag, as you suggest.
@Kim I explained a simple way to flag that. When you detect a decimal point, set your flag to true, on subsequent detections you the behave as if its not valid.
0

Use a MaskedTextBox instead and set the mask to only accept decimals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.