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

Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Dec 8, 2008, 8:01 PM EST
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;
}
}
Aleksandar Hristovski25 reputationPosted Dec 8, 2008, 9:19 PM EST
Thanks!
Ryan AlfordRank #1,411 · 1.2K reputationPosted Dec 9, 2008, 9:07 AM EST
Aleksandar Hristovski25 reputationPosted Dec 9, 2008, 4:21 PM EST
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Dec 9, 2008, 8:40 PM EST
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 Hristovski25 reputationPosted Dec 9, 2008, 8:44 PM EST