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 to the CF scene
    Join Date
    May 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validate price textbox

    Hi there,

    I am very new to javascript, i have got this great bit of code tha allows only numbers to be typed in the text box, how ddo i adjust this code to allow a . as well as the numbers.

    Code:
    <script type="text/javascript">
    function CheckPrice() 
    {
    var key = event.keyCode;
    var keyStr = String.fromCharCode(key); 
    var keyNum = parseInt(keyStr);
    if(isNaN(keyNum)) 
    {
    event.keyCode = ''; 
    }
    }
    </script>
    thanks in advance to anyone that can help.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    Just change

    if(isNaN(keyNum))

    to

    if( isNaN(keyNum) && keyStr != "." )

    But here's a question for you: How do you prevent somebody from using their mouse to copy/paste completely invalid trash in there???

    You can probably tell that I'm not a fan of these filter-on-keystroke systems.

    Oh, and by the VERY VERY BIG by, that code will work *ONLY* in Microsoft Internet Explorer. If you want something similar to work in all other browsers, you'll need a different set of code.

  • #3
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    That "great bit of code" has me stumped there a little; I honestly don't see how it's working at all, except maybe in just the right browser you happen to be using yourself.

    Apart from the fact that you don't have that global event object in every browser, even if you did, many browsers won't just let you set that keyCode property.

    If you're really happy with that piece of code, just replace that condition
    Code:
    if(isNaN(keyNum))
    with
    Code:
    if(isNaN(keyNum) && keyStr != '.')
    If you happen to care about browser compatibility though, use something like that great bit of code:
    Code:
    function CheckPrice(el){
    	el.value = el.value.replace(/[^\d\.]/g, '');
    }
    and call it like this:
    Code:
    <textarea onkeyup="CheckPrice(this)"></textarea>
    (supposing you are using inline event handlers)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    HEH! Much better solution, Venegal.

    Yeah, the code *does* work in MSIE, ugly as it is.

    Of course, even your code doesn't handle the copy-paste-via-mouse-only.

    But there's an easy fix for that:

    <input name="price" onkeyup="CheckPrice(this)" onchange="CheckPrice(this)">

    Now a copy/paste value will *also* get invalid characters zapped.

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I'm inclined to argue that number input fields are typically rather short and seldom copypasted into, and even if they were, nobody in their right minds would do it with their mouse, except it were a malicious attempt to circumvent validation, in which case there are other ways, and server side validation would have to catch it anyways.

    But it's such an easy fix it would be quite silly to argue and not to just do it your way.


  •  

    Posting Permissions

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