Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validation IsNumeric in C#

Validation IsNumeric in C#

Scheduled Pinned Locked Moved C#
csharptutorial
17 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    angelagke
    wrote on last edited by
    #1

    How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

    C A R D S 5 Replies Last reply
    0
    • A angelagke

      How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Three ways 1 - use a MaskedEditBox 2 - write your own handler so the edit box only takes numbers 3 - use Char.IsNumber to check all the characters in the string. Christian Graus - Microsoft MVP - C++

      A 1 Reply Last reply
      0
      • A angelagke

        How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

        A Offline
        A Offline
        alwz_nikhil
        wrote on last edited by
        #3

        Use this public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; } Nikhil

        A D 2 Replies Last reply
        0
        • C Christian Graus

          Three ways 1 - use a MaskedEditBox 2 - write your own handler so the edit box only takes numbers 3 - use Char.IsNumber to check all the characters in the string. Christian Graus - Microsoft MVP - C++

          A Offline
          A Offline
          angelagke
          wrote on last edited by
          #4

          ok, thanks:)

          1 Reply Last reply
          0
          • A alwz_nikhil

            Use this public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; } Nikhil

            A Offline
            A Offline
            angelagke
            wrote on last edited by
            #5

            Thanks for your source code :) just now i also found another way to validate the numeric field, I would like to share it with you : public bool chkNumeric(string strText) { char[] chars = strText.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] > 57 || chars[i] < 48) { return false; } } return true; } angela

            S D 2 Replies Last reply
            0
            • A angelagke

              Thanks for your source code :) just now i also found another way to validate the numeric field, I would like to share it with you : public bool chkNumeric(string strText) { char[] chars = strText.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] > 57 || chars[i] < 48) { return false; } } return true; } angela

              S Offline
              S Offline
              Shajeel
              wrote on last edited by
              #6

              this code wil not handle -ve numbers and number exceeding int max limit Regards Shajeel

              1 Reply Last reply
              0
              • A angelagke

                How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

                R Offline
                R Offline
                rfjeld
                wrote on last edited by
                #7

                Well you could just do like this: try { Convert.ToInt32(txtstring); } catch { MessageBox.Show("You must input a numeric value!"); return; } -- modified at 2:53 Thursday 8th June, 2006

                M 1 Reply Last reply
                0
                • R rfjeld

                  Well you could just do like this: try { Convert.ToInt32(txtstring); } catch { MessageBox.Show("You must input a numeric value!"); return; } -- modified at 2:53 Thursday 8th June, 2006

                  M Offline
                  M Offline
                  Mairaaj Khan
                  wrote on last edited by
                  #8

                  And whats about this... if(!char.IsNumber(e.KeyChar) e.Handled=true; write it in the text control's KeyPress event. _____________________________ Success is not something to wait for, its something to work for.

                  1 Reply Last reply
                  0
                  • A angelagke

                    How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

                    D Offline
                    D Offline
                    Dave Doknjas
                    wrote on last edited by
                    #9

                    A couple of notes about the other replies: 1. You don't want to be catching exceptions as part of the normal logic flow. This is a well-accepted standard in software development. 2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source. For a more robust equivalent to VB's IsNumeric method see: http://www.tangiblesoftwaresolutions.com/Articles/CSharp%20Equivalent%20to%20IsNumeric.htm[^] David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter Instant C++: VB to C++ converter Clear VB: Cleans up VB.NET code

                    M 1 Reply Last reply
                    0
                    • D Dave Doknjas

                      A couple of notes about the other replies: 1. You don't want to be catching exceptions as part of the normal logic flow. This is a well-accepted standard in software development. 2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source. For a more robust equivalent to VB's IsNumeric method see: http://www.tangiblesoftwaresolutions.com/Articles/CSharp%20Equivalent%20to%20IsNumeric.htm[^] David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter Instant C++: VB to C++ converter Clear VB: Cleans up VB.NET code

                      M Offline
                      M Offline
                      Mairaaj Khan
                      wrote on last edited by
                      #10

                      David Anton wrote:

                      2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source.

                      Hi David Anton I didn't understand this point! _____________________________ Success is not something to wait for, its something to work for.

                      D 1 Reply Last reply
                      0
                      • A angelagke

                        How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela

                        S Offline
                        S Offline
                        Suamal
                        wrote on last edited by
                        #11

                        this code may help you to check whether the given value is numeric or not using System.Text.RegularExpressions; public static bool CheckNumeric(string strText) { return CheckRegExPattern(@"^[0-9]+$", strText.Trim()); } -- modified at 7:05 Friday 9th June, 2006

                        M 1 Reply Last reply
                        0
                        • M Mairaaj Khan

                          David Anton wrote:

                          2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source.

                          Hi David Anton I didn't understand this point! _____________________________ Success is not something to wait for, its something to work for.

                          D Offline
                          D Offline
                          Dave Doknjas
                          wrote on last edited by
                          #12

                          Standard Windows copy/paste doesn't trigger KeyPress events. User the TextChanged event to capture pasting of text also. David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter Instant C++: VB to C++ converter Clear VB: Cleans up VB.NET code

                          M 1 Reply Last reply
                          0
                          • A angelagke

                            Thanks for your source code :) just now i also found another way to validate the numeric field, I would like to share it with you : public bool chkNumeric(string strText) { char[] chars = strText.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] > 57 || chars[i] < 48) { return false; } } return true; } angela

                            D Offline
                            D Offline
                            Dustin Metzgar
                            wrote on last edited by
                            #13

                            You're kidding right?

                            1 Reply Last reply
                            0
                            • A alwz_nikhil

                              Use this public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; } Nikhil

                              D Offline
                              D Offline
                              Dustin Metzgar
                              wrote on last edited by
                              #14

                              Be careful. The exception police will get you. As has been explained to me, relying on exceptions is bad practice. Use TryParse instead of Parse and it returns a boolean to let you know if it worked or not.

                              1 Reply Last reply
                              0
                              • D Dave Doknjas

                                Standard Windows copy/paste doesn't trigger KeyPress events. User the TextChanged event to capture pasting of text also. David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter Instant C++: VB to C++ converter Clear VB: Cleans up VB.NET code

                                M Offline
                                M Offline
                                Mairaaj Khan
                                wrote on last edited by
                                #15

                                David Anton wrote:

                                Standard Windows copy/paste doesn't trigger KeyPress events. User the TextChanged event to capture pasting of text also.

                                of course! I checked it last day and found the same situation. It means that we can't rely on only keyPress event for validation, we have to check some other events too; like, as you mentioned, TextChanged etc. Thanks. _____________________________ Success is not something to wait for, its something to work for.

                                1 Reply Last reply
                                0
                                • S Suamal

                                  this code may help you to check whether the given value is numeric or not using System.Text.RegularExpressions; public static bool CheckNumeric(string strText) { return CheckRegExPattern(@"^[0-9]+$", strText.Trim()); } -- modified at 7:05 Friday 9th June, 2006

                                  M Offline
                                  M Offline
                                  Mairaaj Khan
                                  wrote on last edited by
                                  #16

                                  Suamal wrote:

                                  CheckRegExPattern

                                  What is this? I didn't find it.

                                  Suamal wrote:

                                  using System.Text.RegularExpressions; public static bool CheckNumeric(string strText) { return CheckRegExPattern(@"^[0-9]+$", strText.Trim()); }

                                  While taking idea from this I tryed to implement it like this: using System.Text.RegularExpressions; public bool CheckNumeric(string strtext) { System.Text.RegularExpressions.Regex rr = new Regex(@"^[0-9]+$"); return rr.IsMatch(strtext); } ///In Button click event MessageBox.Show(this.CheckNumeric(TextBox1.Text).ToString()); Its working, but i'm unable to interpret this @"^[0-9]+$" Thanks and Best Regards _____________________________ Success is not something to wait for, its something to work for.

                                  M 1 Reply Last reply
                                  0
                                  • M Mairaaj Khan

                                    Suamal wrote:

                                    CheckRegExPattern

                                    What is this? I didn't find it.

                                    Suamal wrote:

                                    using System.Text.RegularExpressions; public static bool CheckNumeric(string strText) { return CheckRegExPattern(@"^[0-9]+$", strText.Trim()); }

                                    While taking idea from this I tryed to implement it like this: using System.Text.RegularExpressions; public bool CheckNumeric(string strtext) { System.Text.RegularExpressions.Regex rr = new Regex(@"^[0-9]+$"); return rr.IsMatch(strtext); } ///In Button click event MessageBox.Show(this.CheckNumeric(TextBox1.Text).ToString()); Its working, but i'm unable to interpret this @"^[0-9]+$" Thanks and Best Regards _____________________________ Success is not something to wait for, its something to work for.

                                    M Offline
                                    M Offline
                                    Mairaaj Khan
                                    wrote on last edited by
                                    #17

                                    At last found the site .NET Framework Regular Expressions [^] Thanks God and Thanks to the forum _____________________________ Success is not something to wait for, its something to work for.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups