We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,680 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Numbers-Only Textbox

1
By vckicks on Jun 16th, 2008 12:01 pm

Use regular expressions, regex, to create an efficient textbox that only takes in digits as input.

//Add to the textbox's KeyPress event
private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}

Thanks. Good one. It helped me.

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
Skill Endorsements: 0

this works for integer values, but not for negative and noninteger
numbers

nika0201
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

doesnt work with back space

tmc01
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

for controls like backspace, del, etc... use this:

if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true;

:)

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
Skill Endorsements: 0

Your code does not work papanyquiL, at least on my system that is.
Check out this

ddanbe
Industrious Poster
4,375 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 738
Skill Endorsements: 26

@ddanbe
Maybe you're not using the same event handler that I am.. Here's the complete code

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //makes sure that textBox1 is a number
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar)) e.Handled = true;
        }

It works perfectly on my end.

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
Skill Endorsements: 0

Yes! This works fine!

ddanbe
Industrious Poster
4,375 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 738
Skill Endorsements: 26

Use NumericUpDown control.

Ravenheart
Light Poster
27 posts since Nov 2009
Reputation Points: 11
Solved Threads: 5
Skill Endorsements: 0

how about numeric textbox for vb.net

ai_enma
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@ai_enma
?? This snippet almost reads as vb. Use Sub End Sub instead of curly braces, and an If End If.
If that not helps you, ask in the vb.net forum.

ddanbe
Industrious Poster
4,375 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 738
Skill Endorsements: 26

Simplest method to use a numeric textbox which will accept the 'BackSpace' key also...

int isNum = 0;

if (e.KeyChar == '\b')
     e.Handled = false;
else if (!int.TryParse(e.KeyChar.ToString(), out isNum))
     e.Handled = true;
debasishsahu4u
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please Help !!
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))

What does "\\d+" Means ??

islame-g
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,islame-g, welcome here.
Please don't ressurrect old threads, read the rules!
You better start a new thread instead.
To learn about regular expressions, you could start here: http://www.regular-expressions.info/dotnet.html

ddanbe
Industrious Poster
4,375 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 738
Skill Endorsements: 26

Hi ddnabe,So sorry ,I didn't know about this rule .
Many Thx. for your reply ,I think this link is very useful

islame-g
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Awsome man.. thanxxxx...

tharakauka
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

wow, wonderful areas to read . thanks for every ideas given

sanjeewa.abeywardana
Light Poster
31 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Note : In the keypress event of textbox type the below code


if(((int)e.KeyChar >=48 && (int)e.KeyChar <=57)||(int)e.KeyChar==08)
{

return;

}

e.Handled=true;


//Please note.The Ascii 08 is for allowing Back Space
//by Chathanz.. B-)
Chatthanz
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1115 seconds using 2.72MB