How can I restrict input to a text-box so that it accepts only numbers and the decimal point?
- 
                    http://stackoverflow.com/questions/2808184/restricting-input-to-textbox-allowing-only-numbers-and-decimal-point?rq=1 looks like the one . - `input type="number` might be worth a look too http://caniuse.com/input-number – Rob Sedgwick Apr 12 '14 at 11:16
6 Answers
var textField = Ti.UI.createTextField({
        top : 20,
        left : 10,
        right : 10,
        height : 60,
        keyboardType : Ti.UI.KEYBOARD_NUMBER_PAD
    });
    Window.add(textField);
With the help of property keyboardType : Ti.UI.KEYBOARD_NUMBER_PAD you can restrict input of a text-box so that it accepts only numbers and the decimal point
 
    
    - 1,138
- 8
- 21
Use this:
<html>
    <head>
        <script>
        function removeNumbers(textbox) {
            textbox.value = textbox.value.replace(/[^\0-9]/g, "");
        }
        </script>
    </head>
    <body>
    <input onKeyPress="removeNumbers(this)">
    </body>
</html>
 
    
    - 555
- 2
- 16
- 
                    You would need to have a method that updates everytime a new key is pressed – theonlygusti Apr 12 '14 at 11:25
Here is a jsfiddle made by another user of this site, ShreejiShah: http://jsfiddle.net/4HHeQ/ Here is the original question: How to make HTML input tag only accept numbers?
It uses jQuery to detect keystrokes,
jQuery("#myId").keydown(function(event) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ( jQuery.inArray(event.keyCode,[46,8,9,27,13,190]) !== -1 ||
             // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) || 
             // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
               }   
        }
    });
And if the key isn't a number, backspace, delete, tab, escape, enter or . it "prevents default" action, which would be to show the inputted character in the textbox.
Once again, this is not my code, but it is absolutely brilliant, so I use it hopefully without offending anyone.
 
    
    - 1
- 1
 
    
    - 11,032
- 11
- 64
- 119
you can use below function to validate numbers and digit.
function isDigit(number) {
    var letters = /^[0-9. ]+$/;
    var bl = false; 
    if (number.match(letters)) {
        bl = true;
    }
    return bl;
}
Ti.API.info(isDigit("12ass40.12456"));
Ti.API.info(isDigit("12350.12456"));
this is just an example but you can call this function on change event of textField to validate.
 
    
    - 1,217
- 1
- 7
- 8
Here we go! I did this in my code! Enjoy it
<html>
 <head>
  <script>
  function replacePonto(){
    var input = document.getElementById('qtd');
    var ponto = input.value.split('.').length;
    if (ponto > 2)
     input.value=input.value.substr(0,(input.value.length)-1);
    input.value=input.value.replace(/[^0-9.]/,'');
    if (ponto ==2)
     //this limit how much numbers after decimal point
      input.value=input.value.substr(0,(input.value.indexOf('.')+3));
     if(input.value == '.')
      input.value = "";
         }
  </script>
  </head>
  <body>
     <input type="text" id="qtd" maxlength="10" style="width:140px" onkeyup="return replacePonto()">
  </body>
</html>- 
                    `input.value=input.value.replace(/[^0-9.]/,'');`. edited now... now this code is really something!!! – JONAS MAGALHAES DOS SANTOS May 04 '17 at 18:42
Here is perfect for me:
function validateNumberDouble(event) {
    var n = event.key;
    k = event.keyCode;
    v = event.target.value;
    if (k === 8 || k === 37 || k === 39 || k === 9 || k === 13) {
        return true;
    } else {
        var dot = v.split('.').length;
        if ((dot > 1 && n == '.') || (v.length == 0 && dot >= 1 && n == '.')) {
            return false;
        } else {
            if (n.match(/[^0-9.]/)) {
                return false;
            }else{
                if(v.indexOf('.')>0 && v.length - v.indexOf('.')>=3){
                    return false;
                }
            }
        }
    }
}
 
    
    - 1
- 1
 
    