469,282 Members | 2,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

numeric validation

how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box


can anyone provide a snippet of code please

many thanks
May 17 '07 #1
3 2114
Frinavale
9,735 Expert Mod 8TB
how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box


can anyone provide a snippet of code please

many thanks
Is this a desktop application or web application.
If it's a web application it depends on what you're trying to do...do you want to just inform the user that the value they entered was not a number?

Or do you want to prevent them from even entering numbers?
This method requires a Client side JavaScript that checks for you...but you'll still have to check Server side (in your code behind) to make sure it is a number. Otherwise you could run into some problems.

-Frinny
May 17 '07 #2
its very simply , just use a client side script that mask over the key press
have a look

function maskNumeric(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if ((charCode < 48 || charCode > 57) )
return false;
return true;
}


no call this function on click event
like
onKeyPress="return validTaskCode(event)"
May 23 '07 #3
Frinavale
9,735 Expert Mod 8TB
its very simply , just use a client side script that mask over the key press
have a look
Expand|Select|Wrap|Line Numbers
  1. function maskNumeric(evt)
  2. {        
  3. var charCode = (evt.which) ? evt.which : event.keyCode
  4. if ((charCode < 48 || charCode > 57) )
  5.     return false;
  6. return true;
  7. }
  8.  
no call this function on click event
like
onKeyPress="return validTaskCode(event)"
Thanks!
Abdul Haque has given a great example on how to JavaScript to prevent people from entering anything other than a number.

I strongly suggest that you still make sure that this is a number when the form is submitted to the server. People can get around JavaScript and submit invalid.

Just do a "Try, Catch" around an Integer.Parse(textBox.Text)...if it throws an exception then they've not provided a number.

-Frinny
May 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

30 posts views Thread by Dr John Stockton | last post: by
16 posts views Thread by Keith | last post: by
16 posts views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.