2

There'a way in javascript to allow the user input in a text field (input type="text") only digits but optionally having the minus before them? (I.e. only negative and positive number)

Sasha Grievus
  • 2,566
  • 5
  • 31
  • 58

5 Answers5

2
<input type='text' onkeypress='return numbersOnly(this,event,false,true);'>

function numbersOnly(Sender,evt,isFloat,isNegative) {
            if(Sender.readOnly) return false;       

            var key   = evt.which || !window.event ? evt.which : event.keyCode;
            var value = Sender.value;

            if((key == 46 || key == 44) && isFloat){                
                var selected = document.selection ? document.selection.createRange().text : "";
                if(selected.length == 0 && value.indexOf(".") == -1 && value.length > 0) Sender.value += ".";
                return false;
            }
            if(key == 45) { // minus sign '-'
                if(!isNegative) return false;
                if(value.indexOf('-')== -1) Sender.value = '-'+value; else Sender.value = value.substring(1);
                if(Sender.onchange != null) {
                    if(Sender.fireEvent){
                        Sender.fireEvent('onchange');
                    } else {
                        var e = document.createEvent('HTMLEvents');
                            e.initEvent('change', false, false);
                        Sender.dispatchEvent(e);
                    }
                }

                var begin = Sender.value.indexOf('-') > -1 ? 1 : 0;
                if(Sender.setSelectionRange){
                    Sender.setSelectionRange(begin,Sender.value.length);
                } else {
                    var range = Sender.createTextRange();
                    range.moveStart('character',begin);
                    range.select();                 
                }

                return false;
            }
            if(key > 31 && (key < 48 || key > 57)) return false;
        }

Related question: HTML Text Input allow only Numeric input

Community
  • 1
  • 1
Jan Pfeifer
  • 2,854
  • 25
  • 35
1

You can bind a listener (in javascript, that is) to the onKeyUp event (or the one that best fits your needs) and check what is the user trying to insert. Then you can decide if you'll let him insert that character or not.

NOTE: You'll want to control also the copy/paste event as the user could paste some text instead of writing it!

alexandernst
  • 14,352
  • 22
  • 97
  • 197
1

Use this. It works.

https://raw.github.com/SamWM/jQuery-Plugins/master/numeric/jquery.numeric.js

$(document).ready(function(){
    $(".numeric").numeric({negative :true});
});​
kjana83
  • 398
  • 3
  • 16
0

Try the following regexp:

/^-?\d+$/
Artem Vyshniakov
  • 16,355
  • 3
  • 43
  • 47
  • The problem of the regexp is i'm using it with the function replace which control the input letter by letter and i risk to accept input like this 4-53. The control i wish to do had to be done while the user types not after like a validation. Thank you anyway :) – Sasha Grievus Nov 09 '12 at 10:19
0

This is the best solution I came up with for positive and negative integers only

<input type="text" onkeyup="this.value = (this.value[0] === '-') ? ('-' + this.value.replace(/[^0-9]/g, '')) : (this.value.replace(/[^0-9]/g, ''));" pattern="^-?\d+" />

You can check it out here http://jsfiddle.net/4g08ofz6/

 

The pattern

pattern="^-?\d+"

is just an extra layer of validation for when the value is pasted in or dragged in but remember you should still always validate on the server site

Carlos E Silva
  • 1,001
  • 1
  • 7
  • 5