Forums » .NET » JavaScript »

How to allow numeric characters on keypress event?


Posted Date: 03 Sep 2013      Posted By:: Jayesh     Member Level: Bronze    Member Rank: 3691     Points: 1   Responses: 4



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





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.

Responses

#727397    Author: Danasegarane.A      Member Level: Gold      Member Rank: 59     Date: 03/Sep/2013   Rating: 2 out of 52 out of 5     Points: 0

Refer :

http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input

Thanks & Regards,
Danasegarane Arunachalam

http://www.techreceipe.com/


 
#727398    Author: Danasegarane.A      Member Level: Gold      Member Rank: 59     Date: 03/Sep/2013   Rating: 2 out of 52 out of 5     Points: 0

And this one

http://www.codeproject.com/Tips/328178/Allow-only-Numeric-values-in-ASP-Text-box-control

Thanks & Regards,
Danasegarane Arunachalam

http://www.techreceipe.com/


 
#727400    Author: Jivani      Member Level: Gold      Member Rank: 221     Date: 04/Sep/2013   Rating: 2 out of 52 out of 5     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;
}
}

http://www.codeproject.com/Articles/220519/Numbers-or-Characters-only-Textbox-Validation-in-C

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

php-wordpress Developer
you can any problem to php wordpress to contact me
jivanibhai7306@gmail.com
Thanks!!...





 
#727455    Author: Sumit Pyne      Member Level: Silver      Member Rank: 702     Date: 04/Sep/2013   Rating: 2 out of 52 out of 5     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();
}
}



Regards,
Sumit


 
Post Reply

 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.



Next : How to stop javascript code execution after alert message?
Previous : How to work with Knockout.js examples?
Return to Discussion Forum
Post New Message
Category: JavaScript

Related Messages

Subscribe to Subscribers
Awards & Gifts
Talk to Webmaster Tony John

Online Members

Gopi A
More...
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India