Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get use ng-pattern

I have a simple text input in which I only want to allow floats and ints (watch out: jade)

input.form-control(type="text", ng-model='usd', ng-pattern="nums",ng-change='convert_to_btc()', placeholder="USD")

However it doesn't work, I can always insert any character in the input (do I need to do more in order to display something? e.g. a red border if it's incorrrect? or should then just those characters not even be able to be entered?) The pattern is a regex and thus not a string, so that should be fine???

Here's the controller:

app.controller("AppCtrl", function AppCtrl($scope, $http, $interval ) {
    //lots of other stuff
    $scope.nums = /^\-?\d+((\.|\,)\d+)?$/; //note no string, it's a  regex
}

This is the generated HTML. Could this be the problem? The generated HTML actually has a string, not a regex!?

<input type="text" ng-model="usd" ng-pattern="/^\-?\d+((\.|\,)\d+)?$/" ng-change="convert_to_btc()" placeholder="USD" class="form-control ng-dirty ng-valid-parse ng-touched ng-invalid ng-invalid-pattern">
like image 814
transient_loop Avatar asked Feb 20 '15 01:02

transient_loop


People also ask

How to use ng pattern in Angular?

The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ngModel on input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ngpattern attribute value.

How do you use ng pattern restrict?

The ng-pattern-restrict directive is used to restrict the user input by disabling the invalid characters with the help of Regular Expression patterns in AngularJS. The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.

How do you use NG on change?

Definition and UsageThe ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

On which of the architectural pattern AngularJS is based?

Explanation: The MVC, Model view controller is a pattern on which angular is based. MVC is a software design pattern that is used for developing the UI. So the architecture pattern that angular is based on MVC.


2 Answers

I hope this is what you are trying to do.

Please have a look at the below link

http://plnkr.co/edit/BGzLbQHy0ZtHYmom8xA3

<!DOCTYPE html>
<html ng-app="">

<head>
 <script data-require="[email protected]" src="https://code.angularjs.org/1.3.13/angular.js" data-semver="1.3.13">      
 </script>
  <style>
    .ng-invalid-pattern {
      border:1px solid #f00;
    }
  </style>
</head>

<body>
  <p>Hello</p>

  <form name='myform'>      
    <input type="text" name='ip' ng-model="usd" ng-pattern="/^\-?\d+((\.|\,)\d+)?$/" 
  ng-change="convert_to_btc()" placeholder="USD"/>
    <p ng-show='myform.ip.$invalid'>Error</p>
  </form>


  </body>

</html>
like image 163
Rajeshwar Avatar answered Sep 29 '22 09:09

Rajeshwar


If you are trying to block the user from being able to enter character/letters and only allowing them to enter numbers into the input, then change the <input type="text" to <input type="number"

Here's a link to the Angular Doc page on inputs that should only allow numbers: input[number]

like image 32
Elliott Avatar answered Sep 29 '22 09:09

Elliott