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
    Jul 2009
    Location
    Odense, Denmark
    Posts
    72
    Thanks
    4
    Thanked 0 Times in 0 Posts

    onChance-function for textinput to regulate characters

    Hi there,


    I need a bit of javascript to solve a problem I'm having.

    I have an HTML text-input field, and I would like - simply - that it be impossible to enter a period-character (.).

    My best take at it so far is a javascript function called by the onChance-event attached to the <input>-tag, but to be honest, nothing about it really works, so I thought I'd let the pros get their take at it.

    Can somebody help with this?


    //deafdigit

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <input type = "text" id = "myfield" onkeyup = "stopDot()" onblur = "stopDot()">
    
    <script type = "text/javascript">
    function stopDot(){
    var val = document.getElementById("myfield").value;
    val = val.replace(/\./g,"");
    document.getElementById("myfield").value = val;
    }
    </script>
    Note that you need to block the user pasting in a . character. (onblur)

    Why do you want to do this? Surely not to force an integer number??

    Surely you mean onchange - not onchance!!


    "If you think education is expensive, try ignorance." - Derek Bok (1930-), Harvard University President
    Last edited by Philip M; 10-05-2011 at 11:54 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jul 2009
    Location
    Odense, Denmark
    Posts
    72
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Philip,


    First of all - yes onchange ofcourse. For some reason my fingers keep typing "onchance" instead.

    I want this because I'm danish and I need my users to enter a currency-amount. In danish, we don't enter large numbers as: 1,234,234.97 but rather as: 1.234.234,97 which means my DB efs up everytime someone enters a number like 1.234,97 (it only handles so many decimals). Therefore I need my input field to ignore period-characters.

    I stumpled upon this function:

    Code:
    <form name="InputLimiter">
    
    <p>Numbers Only: <input type="text" id="NumbersOnly" onkeypress="return inputLimiter(event,'Numbers')" value="1234567890" /></p>
    
    </form>
    
    <script type="text/javascript" language="javascript">
    
    function inputLimiter(e,allow) {
    var AllowableCharacters = '';
    
    if (allow == 'Numbers'){AllowableCharacters='1234567890,';}
    
    var k;
    k=document.all?parseInt(e.keyCode): parseInt(e.which);
    if (k!=13 && k!=8 && k!=0){
    if ((e.ctrlKey==false) && (e.altKey==false)) {
    return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
    } else {
    return true;
    }
    } else {
    return true;
    }
    }
    </script>
    Which seems to do the job. Any thoughts?


    //deafdigit

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    That is a very complicated and inefficient way to enter numbers only. If you had made this requirement clear I would have offered you

    Code:
    <input type = "text" id = "myfield" onkeyup = "stopDot()" onblur = "stopDot()">
    
    <script type = "text/javascript">
    function stopDot(){
    var val = document.getElementById("myfield").value;
    val = val.replace(/[^0-9\,]/g,"");  // only numbers and comma allowed
    document.getElementById("myfield").value = val;
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Jul 2009
    Location
    Odense, Denmark
    Posts
    72
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks again, Philip. Your function did the trick. Very nice.

    I'll be able to do this in the future.


  •  

    Posting Permissions

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