TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Allows Only Numbers to be Typed into a Text Box
Nemilidinne Ashokreddy
Nov 24
2015
Code
749
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Html
<input type=
"text"
name=
"number"
only-Numbers>
AngularJS Code
.directive(
'onlyDigits'
, function()
{
return
{
require:
'ngModel'
,
restrict:
'A'
,
link: function(scope, element, attr, ctrl)
{
function inputValue(val)
{
if
(val)
{
var digits = val.replace(/[^0-9.]/g,
''
);
if
(digits !== val)
{
ctrl.$setViewValue(digits);
ctrl.$render();
}
return
parseFloat(digits);
}
return
undefined;
}
ctrl.$parsers.push(inputValue);
}
};
});
angularjs