CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2011
    Posts
    5

    Testing for non numeric data but must allow decimal point

    I am trying to check for non numeric data however the user must be able to use decimal points, I have the following code so far but this does not allow decimal point, could someone advise how I would go about this please?

    Thanks in advance.

    Code:
    // non numeric data check
                int num1;
                bool isNumeric1 = int.TryParse(txtHeight.Text, out num1);
                if (isNumeric1)
                {
    
                }
                else
                {
                    txtHeight.Text = ("");
                    MessageBox.Show("Only enter numbers into\nthe Height text field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    
                // non numeric data check
                int num2;
                bool isNumeric2 = int.TryParse(txtWeight.Text, out num2);
                if (isNumeric2)
                {
                    // convert and display conversion in appropriate textfield, call class to convert
                }
                else
                {
                    txtWeight.Text = ("");
                    MessageBox.Show("Only enter numbers into\nthe Weight text field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

  2. #2
    Join Date
    Aug 2008
    Posts
    78

    Re: Testing for non numeric data but must allow decimal point

    I would do it with RegEx. I think this should work.
    But to be hones, I could not understand your question. You meant
    (1)non-numerc data and/with decimal point should be allowed
    (2)numeric data with decimal point should be allowed
    Writing solution for both. May be true and false which is returned by the expression should be interchanged.

    Code:
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Regex.IsMatch(yourString, @"\A(\D+)\Z"); //(1)
                Regex.IsMatch(yourString, @"\A(\d+.{0,1})\Z");//(2)
            }
        }
    }
    Be careful about capital and small d in pattern.
    Last edited by ricky_cpp; July 5th, 2011 at 09:31 AM.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Testing for non numeric data but must allow decimal point

    Are you saying that you want to support numbers like "5" as well as "5.5"? If so, then int.Parse is the wrong thing to use as it only parses valid *integers*. If you want to parse valid floating point numbers use double.Parse/float.Parse. You can control exactly what styles of floating point numbers are allowed by using the Parse overload which takes a "NumberStyles" parameter. Regex is complete overkill for such a simple task.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Testing for non numeric data but must allow decimal point

    Actually I'm fairly sure that winforms comes with a few built in validators which should do the task easily enough for you. http://blog.scosby.com/post/2010/02/...ows-Forms.aspx
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Jun 2011
    Posts
    5

    Re: Testing for non numeric data but must allow decimal point

    Thanks for the info, yes I want my conversions to allow whole numbers as well as decimal points but not allow the obvious such as letters in a number.

    I will post back upon the issue being resolved.

  6. #6
    Join Date
    Jun 2011
    Posts
    5

    Re: Testing for non numeric data but must allow decimal point

    I have tried

    Code:
    double dOutput = 0;
                if (Double.TryParse(txtHeight.Text, out dOutput))
                {
                    MessageBox.Show("Valid double");
                }
                else
                {
                    MessageBox.Show("Only enter numbers and decimal point where necessary");
                }
    but this does not catch non numeric data, am I missing the point?

  7. #7
    Join Date
    Aug 2008
    Posts
    78

    Re: Testing for non numeric data but must allow decimal point

    It should work. You should also give NumberStyle.AllowDecimalPoint (or something, dont remember what exactly it was)

    double.Pasre(yourString, NumberStyleFormat.AllowDecimalPoint)

    Catch exception for anything other that number

  8. #8
    Join Date
    Aug 2008
    Posts
    78

    Re: Testing for non numeric data but must allow decimal point

    Also check your culture settings
    or

    double.TryParse(yourString, NumberStyles.AllowDecimal, CultureInfo.InvariantCulture, out yourNumber);

  9. #9
    Join Date
    May 2007
    Posts
    1,546

    Re: Testing for non numeric data but must allow decimal point

    "but this does not catch non numeric data, am I missing the point? "

    What doesn't it catch? If Double.TryParse returns true, it was a valid number and you're just mistaken.

    Also check your culture settings
    or

    double.TryParse(yourString, NumberStyles.AllowDecimal, CultureInfo.InvariantCulture, out yourNumber);
    As long as your application is only ever run in a culture which uses a comma to separate thousands and a full stop for the fractional part, you won't have an issue with the InvariantCulture. As soon as you want to run your app in France, it'll crash and blow up. You should run on the assumption that user input will always be in the current culture. i.e. a french guy will input in the normal french way, an irish guy will input in the normal irish way.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  10. #10
    Join Date
    Aug 2008
    Posts
    78

    Re: Testing for non numeric data but must allow decimal point

    I am Programmer in Germany but I use English culture for most of my application. My system default is DE, so I have to be careful about it while programming. That was why I made this suggestion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured