1
votes

How can I restrict input to a text-box so that it accepts only numbers and the decimal point?

6
stackoverflow.com/questions/2808184/… looks like the one . - input type="number might be worth a look too caniuse.com/input-numberRob Sedgwick

6 Answers

3
votes
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

0
votes

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.

0
votes

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.

0
votes

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>
0
votes

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>
0
votes

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;
                }
            }
        }
    }
}

https://jsfiddle.net/renatoalexandro/rdeu4n1m