Re: [angular.js] Restricting input

1,864 views
Skip to first unread message

Josh Kurz

unread,
Jun 28, 2012, 11:55:06 AM6/28/12
to ang...@googlegroups.com
1. You could write a filter to restrict the input. 

angular.module('myApp.filters', []).
    filter('restrict',  function () {
        return function(input) {
	  if(input.isNumber)
              return input;
          else
              return 'must enter a number';
                     
     }
    });


2. you could write a filter that does something like this for the comma question. 

//I just took the logic from http://www.mredkj.com/javascript/nfbasic.html
angular.module('myApp.filters', []).
    filter('comma',  function () {
        return function(input) {
                      input += '';
	x = input.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
        }
    });


Or were you just asking to add these types of filters to the Angular.JS library? 


On Thu, Jun 28, 2012 at 10:31 AM, Teren Teh <ter...@gmail.com> wrote:
Hey there, 

I'm wondering if AngularJS provides anything that allows me to restrict input in a textbox? I've had a look at the API and the regex only performs validation - does it also do restrictions? I'd also like to add something that converts the user's input (i.e. when they enter a number greater than 1000, a comma gets added like 1,000).

Thanks!

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/XsCMkGKr0zcJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.



--
Josh Kurz

Head of Web Development
SEO
Social Media 
Digital Photography 
Internet Marketing

cell#      404-641-7807
office#    404-593-0851
fax#       404-506-9642
Dillinger Media 
http://dillingermediaonline.com


Peter Bacon Darwin

unread,
Jun 28, 2012, 12:34:30 PM6/28/12
to ang...@googlegroups.com
Have a look at ngModelController's $parsers and $formatters.  http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController
Pete

On 28 June 2012 17:27, Teren Teh <ter...@gmail.com> wrote:
Thanks Josh. That's perfect!

I wasn't sure if there was already something that did this. And I wasn't sure how to go about doing it - I initially thought of doing it with ng-change but filters sound like a better idea. 
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.



--
Josh Kurz

Head of Web Development
SEO
Social Media 
Digital Photography 
Internet Marketing

cell#      404-641-7807
office#    404-593-0851
fax#       404-506-9642
Dillinger Media 
http://dillingermediaonline.com


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/iaEag74qE3kJ.

fkra...@googlemail.com

unread,
Nov 12, 2012, 1:08:16 AM11/12/12
to ang...@googlegroups.com
How can I enable the filter for a ng-model binding?
Reply all
Reply to author
Forward
0 new messages