Skip to content

Commit 9510305

Browse files
author
Nitish
committed
Version 1.1 released
1 parent 59edc50 commit 9510305

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

README.md

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ A angularjs directive which allows only numbers to be typed into a text box .
1010
* Accepts only numbers.
1111
* Accepts negative number.
1212
* Accepts floating Number
13+
* Control over Decimal Number - Add only numbers not floating number
14+
* Control over Negative Number - Can't add negative number
15+
* Decimal place upto
1316

1417

1518
## DEMO
@@ -22,10 +25,27 @@ A angularjs directive which allows only numbers to be typed into a text box .
2225
---------------
2326

2427
```
25-
just add "nks-only-number".
26-
<input type="text" nks-only-number ng-model="mynumber"/>
28+
just add "nks-only-number" and also include required parameter.
29+
30+
<input type="text" nks-only-number ng-model="mynumber" />
31+
32+
<input type="text" nks-only-number ng-model="mynumber" decimal-upto="2" />
33+
34+
<input type="text" nks-only-number ng-model="mynumber" decimal-upto="2" allow-negative="false" />
35+
36+
<input type="text" nks-only-number ng-model="mynumber" allow-decimal="false" />
2737
```
2838

39+
40+
## PARAMETERS
41+
---------------
42+
43+
* decimal-upto="2" //value will be number. -- Restrict decimal upto
44+
* allow-negative="false" // if donot want negative number put false as value otherwise donot include it
45+
* allow-decimal="false" // if donot want decimal number put false as value otherwise donot include it
46+
47+
48+
2949
#### Directive Code
3050
--------------------
3151

@@ -37,16 +57,44 @@ A angularjs directive which allows only numbers to be typed into a text box .
3757
restrict: 'EA',
3858
require: 'ngModel',
3959
link: function (scope, element, attrs, ngModel) {
40-
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
41-
var spiltArray = String(newValue).split("");
42-
if (spiltArray.length === 0) return;
43-
if (spiltArray.length === 1 && (spiltArray[0] == '-' || spiltArray[0] === '.' )) return;
44-
if (spiltArray.length === 2 && newValue === '-.') return;
45-
60+
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
61+
var spiltArray = String(newValue).split("");
62+
63+
if(attrs.allowNegative == "false") {
64+
if(spiltArray[0] == '-') {
65+
newValue = newValue.replace("-", "");
66+
ngModel.$setViewValue(newValue);
67+
ngModel.$render();
68+
}
69+
}
70+
71+
if(attrs.allowDecimal == "false") {
72+
newValue = parseInt(newValue);
73+
ngModel.$setViewValue(newValue);
74+
ngModel.$render();
75+
}
76+
77+
if(attrs.allowDecimal != "false") {
78+
if(attrs.decimalUpto) {
79+
var n = String(newValue).split(".");
80+
if(n[1]) {
81+
var n2 = n[1].slice(0, attrs.decimalUpto);
82+
newValue = [n[0], n2].join(".");
83+
ngModel.$setViewValue(newValue);
84+
ngModel.$render();
85+
}
86+
}
87+
}
88+
89+
90+
if (spiltArray.length === 0) return;
91+
if (spiltArray.length === 1 && (spiltArray[0] == '-' || spiltArray[0] === '.' )) return;
92+
if (spiltArray.length === 2 && newValue === '-.') return;
93+
4694
/*Check it is number or not.*/
4795
if (isNaN(newValue)) {
48-
ngModel.$setViewValue(oldValue);
49-
ngModel.$render();
96+
ngModel.$setViewValue(oldValue);
97+
ngModel.$render();
5098
}
5199
});
52100
}
@@ -59,8 +107,6 @@ A angularjs directive which allows only numbers to be typed into a text box .
59107
-----------------------------------
60108

61109
* Min-Max for number
62-
* Control over Decimal Number - Add only numbers not floating number
63-
* Control over Negative Number - Can't add negative number
64110

65111

66112

accept-number-only.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,40 @@ angular.module('mainApp', [])
55
restrict: 'EA',
66
require: 'ngModel',
77
link: function (scope, element, attrs, ngModel) {
8-
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
8+
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
99
var spiltArray = String(newValue).split("");
10-
if (spiltArray.length === 0) return;
11-
if (spiltArray.length === 1 && (spiltArray[0] == '-' || spiltArray[0] === '.' )) return;
12-
if (spiltArray.length === 2 && newValue === '-.') return;
13-
10+
11+
if(attrs.allowNegative == "false") {
12+
if(spiltArray[0] == '-') {
13+
newValue = newValue.replace("-", "");
14+
ngModel.$setViewValue(newValue);
15+
ngModel.$render();
16+
}
17+
}
18+
19+
if(attrs.allowDecimal == "false") {
20+
newValue = parseInt(newValue);
21+
ngModel.$setViewValue(newValue);
22+
ngModel.$render();
23+
}
24+
25+
if(attrs.allowDecimal != "false") {
26+
if(attrs.decimalUpto) {
27+
var n = String(newValue).split(".");
28+
if(n[1]) {
29+
var n2 = n[1].slice(0, attrs.decimalUpto);
30+
newValue = [n[0], n2].join(".");
31+
ngModel.$setViewValue(newValue);
32+
ngModel.$render();
33+
}
34+
}
35+
}
36+
37+
38+
if (spiltArray.length === 0) return;
39+
if (spiltArray.length === 1 && (spiltArray[0] == '-' || spiltArray[0] === '.' )) return;
40+
if (spiltArray.length === 2 && newValue === '-.') return;
41+
1442
/*Check it is number or not.*/
1543
if (isNaN(newValue)) {
1644
ngModel.$setViewValue(oldValue);

0 commit comments

Comments
 (0)