views:

80

answers:

5

Hello everyone,

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 :)

+1  A: 

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

Adam
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.
Kim Rivera
+1 for the Numeric Text Box sample. It still needs the "I already have a decimal separator" flag, as you suggest.
Robert Harvey
@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.
Adam
+2  A: 

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);
    }
Luiscencio
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. :(
Kim Rivera
@Kim Rivera: there you go....
Luiscencio
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 Rivera
@Kim careful however, this is not localised - some countries use commas for decimal places.
Adam
@Adam, yes I know that but this software i'm working on is just a small exercise for my class. Thanks for reminding :)
Kim Rivera
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
Luiscencio
commas for decimal???? thats heresy and witchcraft
Luiscencio
Kim Rivera
@Kim Rivera: basically it says: if the key pressed is backspace or '.' and there is no '.' in the textbox's text then process the keypress, if the above statement is false then we try to parse the pressed key as a decimal number and if we succed then whe handle the keypress, if it fails it will ignore the keypress. hpe its clear =]
Luiscencio
ohhhhh... so what should i modify in order for this code to accept the backspace key even if there is already a '.' on the textbox. coz the way i see it, it can only accept the backspace key if there is no '.' on the textbox yet. hehe
Kim Rivera
@Kim Rivera: hahah you are right.. didn't see that coming =P I fixed it now
Luiscencio
thanks, wait. :D what did you do to fix it? i wanna know :D hehehe. thanks
Kim Rivera
Luiscencio
ohhhh.. that explains it. :D thanks :)
Kim Rivera
+1  A: 

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

code4life
+3  A: 
decimal value;
bool isValid = decimal.TryParse(textBox.Text, out value);

if (!isValid)
{
    throw new ArgumentException("Input must be a decimal value");
}
Anthony Pegram
+1 Canonical ---
BC
Localisable, understandable, +1able
Adam
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
Kim Rivera
A: 

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

SnOrfus