Validation IsNumeric in C#
-
How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela
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++
-
How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela
Use this
public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; }
Nikhil -
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++
-
Use this
public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; }
NikhilThanks 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
-
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
-
How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela
-
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
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.
-
How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela
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
-
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
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.
-
How to check validation numeric field in C# coz C# doesn't has built-in IsNumeric function :( Angela
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
-
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.
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
-
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
You're kidding right?
-
Use this
public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; }
NikhilBe careful. The exception police will get you. As has been explained to me, relying on exceptions is bad practice. Use
TryParse
instead ofParse
and it returns a boolean to let you know if it worked or not. -
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
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.
-
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
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. -
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.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.