Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    javascript input validation

    Anyone know how do I use javascript to limit the numeric amount enetered in a text box less than 1000?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    x < 1000 ?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    here's my code so far, we are meant to make the amount entered in the text box be numeric, not negative and no greater than 1000.

    this is the javascript
    Code:
    <script type="text/javascript">
          function numeric(e)
         {
         if ((e.keyCode ==8) || (e.keyCode > 47 && e.keyCode < 58) || (e.keyCode == 110))
         return;
         alert('Please enter a valid value');
         }
          </script>
    and this is inside the <body>
    Code:
    <li>\How much would you be willing to pay?
               $<input class="blue" type="text" name="money" onkeydown="return numeric(event)"/>
    
           </li>
    this only allow the textbox to enter numeric value... you can still type in number greater than 1000.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    I’d rather go checking the input’s value.
    Code:
    var x = parseFloat(input.value);
    if (isFinite(x) && x >= 0 && x < 1000 && (x % 1) === 0) { … }
    of course you can combine both and you only have
    Code:
    if (parseFloat(input.value) < 1000)
    left
    Last edited by Dormilich; 04-15-2010 at 12:57 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    It will be easier to do client side validations using this JavaScript form validation script

    for example
    Code:
    frmvalidator.addValidation("money","lessthan=1000");


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •