Hi,
I'm a newbie using Visual C# 2008, and i ran into a problem.
i need a textbox to contain only numbers, and i don't know how to stop the user from inputing other characters(except adding a label that says "don't input other characters!" :)).
also i don't know how to check what's already inputed by a user into a textbox(i'd normally check the ascii values of the characters, but i'm sure C# has it's own way).
Thanks,
hristo
Loading
Aleksandar HristovskiPosted Dec 10, 2008, 1:44 AM
Niradhip ChakrabortyPosted Dec 10, 2008, 1:40 AM
Try this:
private void txtZip_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
Aleksandar HristovskiPosted Dec 9, 2008, 9:21 PM
Ryan AlfordPosted Dec 9, 2008, 2:07 PM
Aleksandar HristovskiPosted Dec 9, 2008, 2:19 AM
Thanks!
Niradhip ChakrabortyPosted Dec 9, 2008, 1:01 AM
Use the following java script function and call it on keypress event of textbox.
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
alert("Please enter digits");
return false;
}
else{
return true;
}
}