How to validate value 1 and 1.00 as same and 1.01 as different in angular

804 Views Asked by At

How to treat 1 and the number of decimals should be zero eg) 1.000 as same show the alert pop up number should be same.And the maximum number length of the texbox is 7.

eg)1 and 1.00000001 be different

And the number be 1 and 1.01 as different.Here I have numeric value 1 and numeric value 1. any decimal number.

if the number is 1 and 1.00 means show alert value same and 1.01, or 1.001 or any decimals after number it will be considered as different.

Here is my sample code:

       <!doctype html>
        <html ng-app>
          <head>
            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
            <script src="script.js"></script>
          </head>
          <body>
        <div ng-controller="Ctrl">
         <input type="text" ng-model="contractDetailsScreen.percent"  maxlength="7" numbers-only="numbers-only" />  
          <button ng-click="actionme()">click</button>
        </div>
          </body>
        </html>



function Ctrl($scope) {
  $scope.actionme = function(){

    if($scope.contractDetailsScreen.percent){

      alert('value same');
    }
else{
  alert("value diffrent");
}
  };
}

Demo: http://plnkr.co/edit/nxRca6HRQGkVO18c3uXa?p=preview

1

There are 1 best solutions below

0
On BEST ANSWER

use parseFloat to convert the number and then match the value

Controller Code

$scope.actionme = function(){

    var num = parseFloat ($scope.contractDetailsScreen.percent);
         if(1 == num) {
           $scope. result = "Matched";

         } else {
           $scope.result = "not Matched";
         }
      };

Working Demo