allows only numbers to be typed into a text box

14,689 views
Skip to first unread message

Syed Ali

unread,
Apr 18, 2013, 3:14:35 PM4/18/13
to ang...@googlegroups.com
in angular is there any functionality available that allows only numbers to be typed into a text box like  ??

Sebastian Sebald

unread,
Apr 18, 2013, 3:22:11 PM4/18/13
to ang...@googlegroups.com

Syed Ali

unread,
Apr 18, 2013, 4:03:03 PM4/18/13
to ang...@googlegroups.com
Duuudddde :) i've already read this..this is not what i want :)
i want this functionality http://www.texotela.co.uk/code/jquery/numeric/

ng.directive:input.number allows you to enter alphabets as well in text field,its only kind of validation.I want that user can only enter numbers in text field,disallows to enter anything else than valid number/integer.

Sebastian Sebald

unread,
Apr 18, 2013, 4:04:30 PM4/18/13
to ang...@googlegroups.com
I see. No Angular doesn't have such a feature. You have to write your own directive or just wrap the numeric plugin you already have.
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/5o6OepuutbQ/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Bielski

unread,
Apr 18, 2013, 4:25:10 PM4/18/13
to ang...@googlegroups.com
If you're going to write your own directive, you'll need this regex:

.replace(/[^0-9\.]+/g,"")

I run this as part of a larger process that checks to make sure the value is 0 or greater, and less than a value that I set dynamically. It will allow numbers and the decimal point, but replace anything that is not one of those with an empty space. For those that use the comma as a separator, it can be added in with a \, and it should run fine.

Syed Ali

unread,
Apr 19, 2013, 2:44:29 AM4/19/13
to ang...@googlegroups.com
thanks..yea,i wrote my own directive :)

Peter Bacon Darwin

unread,
Apr 19, 2013, 10:03:27 AM4/19/13
to ang...@googlegroups.com
You could also take a look at the Mask directive from angular-ui.github.io/
Pete


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

Nishchit Dhanani

unread,
Nov 14, 2013, 3:46:29 AM11/14/13
to ang...@googlegroups.com
Demo working here

See this derective in angular .

.directive('onlyNum', function() {
        return function(scope, element, attrs) {

            var keyCode = [8,9,37,39,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,110];
            element.bind("keydown", function(event) {
                console.log($.inArray(event.which,keyCode));
                if($.inArray(event.which,keyCode) == -1) {
                    scope.$apply(function(){
                        scope.$eval(attrs.onlyNum);
                        event.preventDefault();
                    });
                    event.preventDefault();
                }

            });
        };
    })

Nitish Kumar

unread,
Sep 3, 2014, 2:47:55 PM9/3/14
to ang...@googlegroups.com

On Friday, April 19, 2013 12:44:35 AM UTC+5:30, Syed Ali wrote:

Ashwini Sharma

unread,
Sep 28, 2014, 7:45:51 AM9/28/14
to ang...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages