numeric validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bipinjohn23
    New Member
    • May 2007
    • 1

    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
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by bipinjohn23
    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

    Comment

    • Abdul Haque
      New Member
      • May 2007
      • 17

      #3
      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="ret urn validTaskCode(e vent)"

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Abdul Haque
        its very simply , just use a client side script that mask over the key press
        have a look
        Code:
        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="ret urn validTaskCode(e vent)"
        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(t extBox.Text)... if it throws an exception then they've not provided a number.

        -Frinny

        Comment

        Working...