1

So I have a Subtotal TextBox where an amount like $546.75 can be entered. Now, I want to make sure that only numbers, ONE decimal, One Dollar Symbol and commas allowed only every 3 places (100,000,000). Is this possible? Maybe not the commas, but at least the numbers, decimal, and dollar symbol.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
cb1295
  • 733
  • 4
  • 18
  • 36

6 Answers6

4

Why you dont put the money sign "$" out side of the textBox (create a label just infrontof textBox), then you will not have to worry about this character, but only about numbers. And it looks better (in my opinion). Then you can use this code:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar != (Char)Keys.Back) //allow backspace (to delete)
        {
            e.Handled = !char.IsNumber(e.KeyChar);
        }
    }
Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
2

All validation should be performed manually on KeyPress event.

Here described validation for numeric values values. You will need to check the '$' sign and decimals additionally.

Community
  • 1
  • 1
Samich
  • 29,157
  • 6
  • 68
  • 77
  • 2
    Though the currency symbol will have to be taken from the current UI culture rather than assumed to be "$" – ChrisF Sep 04 '11 at 18:59
1

//tb - is the name of text box

    private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        char[] inputChar = e.Text.ToCharArray();

        if (char.IsNumber(inputChar[0]))
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }

// another method.

        if (char.IsDigit(inputChar[0]))
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }
Manoj
  • 11
  • 1
1

There are a number of articles on numeric textboxes

Numeric TextBox

http://www.daniweb.com/software-development/csharp/threads/95153

http://www.codeproject.com/KB/vb/NumericTextBox.aspx

I use this one in my projects

http://www.codeproject.com/KB/edit/ValidatingTextBoxControls.aspx

Community
  • 1
  • 1
ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50
1

I think you are using WinForms and not WPF. If that is the case then you could use System.Windows.Forms.ErrorProvider (you can drag-drop one from toolbox to your form) along with regular expressions to do the validation.

WARNING: The regex pattern string below may not do exactly you want but hopefully conveys the idea.

Some match examples... "$4,000.00", "-$4000.00", "-$400.00"

    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        string error = null;
        string pattern = @"^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$";

        if (!Regex.IsMatch(textBox1.Text, pattern))
        {
            error = "Please enter a US currency value.";
            e.Cancel = true;
        }
        errorProvider1.SetError((Control)sender, error);
    }
blins
  • 2,515
  • 21
  • 32
  • Thanks, I am using winforms, dunno where you got WPF from, thanks a bunch, I'll try this out – cb1295 Sep 05 '11 at 02:18
  • Just tried it and it works perfectly and I love how it shows the error icon! Thanks again! – cb1295 Sep 05 '11 at 03:04
  • I saw the winforms tag so I assumed as much but just making sure since your original question does not explicitly state winforms. Glad to hear it works for you! – blins Sep 07 '11 at 21:00
  • I love it how the OP has a comment "Please don't add things like " - WinForm C#" to your titles. That's what we use tags for here" promptly followed by this answer "your original question does not explicitly state winforms" – Ryan Buddicom Apr 25 '13 at 21:59
  • If only there was a way to state things in your question without adding them to your title. – Az- Jan 20 '15 at 20:05
  • @blins I implemented this but I am unable to close the form window unless I clear the invalid text first. Is there a way to make an exception so that the Close button I have does not depend on the validation of the textbox? – erotavlas Apr 10 '18 at 14:07
0

Have you tried Ajax Controls?

http://www.aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx

Simples. :)

Fandango68
  • 4,461
  • 4
  • 39
  • 74