@@ -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
0 commit comments