Search code examples
angularjsvalidationinputng-pattern

Enter a negative number in the input number field and validation with ng-pattern-restrict


I'm having problems with ng-pattern-restrict I have an input number field with ng-pattern-restrict = "^. {0,13}? $" But when I wanted to enter a negative number, I tried two ways:

ng-pattern-restrict = "^ \ - \ d {0,13}? $": This way, the negative number is good, but it does not allow you to enter positive numbers, because the character at the start must be (-)

ng-pattern-restrict = "^ \ d {0,13} (\ - \ d {0,13})? $": this method allows to enter a positive number, but it does not allow you to enter negative numbers, because it requires a number in front Looking forward to having someone help me the other way, Thanks and Best Regards


Solution

  • angular.module('testApp', ['ngPatternRestrict'])
      .controller('TestCtrl', function(){
        console.log('testing')
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
    
    <script
    src="https://cdn.rawgit.com/AlphaGit/ng-pattern-restrict/cb7ba5dd/src/ng-pattern-restrict.js">
    </script>
    <div ng-app="testApp" ng-controller="TestCtrl as tc">
      <input type="text" ng-model="tc.data" ng-pattern-restrict="^\-?\d{0,13}?$"/>
      
      {{tc.data}}
    </div>

    Use a ? for 0 or 1 of a match for the negative