You must Sign In to post a response.

How to allow numeric characters on keypress event?

Are you looking for information on validation on key press event? Want to know how to allow numeric characters on key press event? Read this thread to learn more about validation on key press event with examples.

I have a textbox for entering contact nos. which should accept only numeric values.
How to achieve this (with proper code explanation)?


#727397    Author: Danasegarane.A    03 Sep 2013 Member Level: Gold   Points : 0

Refer :

Thanks & Regards,
Danasegarane Arunachalam

#727398    Author: Danasegarane.A    03 Sep 2013 Member Level: Gold   Points : 0

And this one

Thanks & Regards,
Danasegarane Arunachalam

#727400    Author: Jivani    04 Sep 2013 Member Level: Gold   Points : 2

private void textBox1_TextChanged(object sender, EventArgs e)
if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", textBox1.Text))
MessageBox.Show("Please enter only numbers.");
textBox1.Text.Remove(textBox1.Text.Length - 1);

and another way to created:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
if ( !char.IsDigit(e.KeyChar) )
e.Handled = true;

I Hope To Use Full..
And Choice The Best Answers

php-wordpress Developer
you can any problem to php wordpress to contact me

#727455    Author: Sumit Pyne    04 Sep 2013 Member Level: Silver   Points : 2

You can use the following javascript code in the "onkeypress" event of the textbox.

function IsNumeric() {
var theEvent = window.event;
var key = theEvent.keyCode;
key = String.fromCharCode(key);

var regex = /[0-9]|\./;
if (!regex.test(key)) {
theEvent.returnValue = false;
if (theEvent.preventDefault) theEvent.preventDefault();


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.