AlphaGit / ng-pattern-restrict

Allowing only certain inputs based on a regex pattern, for AngularJS
http://alphagit.github.io/ng-pattern-restrict/
MIT License
47 stars 32 forks source link

NOTE: This project is no longer under maintenance.

However, we will gladly accept new PRs or questions.

ng-pattern-restrict Build Status Build Status

Allowing certain inputs based on a regex pattern, preventing the user from inputting anything invalid.

Build Status

What is this for?

It is possible that at some points you may want to restrict your user from entering certain values in your web application. Specifically, fields that conform to a very strict set of values. If you decide that it is a good idea for you to restrict what the user can even type in the input, this is the tool for you.

This works similar to a masked input, except that no mask is displayed, and the validation is done against a regular expression, which allows you to permit a complex class of values if that's what you need, or to be very specific if that is your necessity.

How does it work?

The logic is quite simple:

Installation

bower install ng-pattern-restrict
# or
npm install ng-pattern-restrict

Alternatively, copy the ng-pattern-restrict.js (or the minified version ng-pattern-restrict.min.js) into your project and reference it in your index.html.

Finally, add a module reference (typically in app.js) to ngPatternRestrict. e.g.:

angular.module('yourApp', [
  // ... other modules ...,
  'ngPatternRestrict']);

Usage

<input type="text" pattern="[0-9]+" ng-pattern-restrict />

Should be the same as

<input type="text" ng-pattern-restrict="[0-9]+" pattern="[0-9]+" />

Or with a scope value

$scope.numbers = "[0-9]+";
<input type="text" ng-pattern-restrict="{{ numbers }}" />

Notes:

If you still have problems, please make sure to check the Compatibility notes. There are several issues that really depend on the browsers.

E2E testing

In order to e2e test you need to execute the following steps:

npm install
./node_modules/protractor/bin/webdriver-manager update

# then, for each time you want to test
npm test

That's it. Neat, huh?

Check out the protractor configuration file for the set of browsers that you want to test on your system.

Demo

What's a library without a demo?

Here's a Plunkr demo for you to check out.

Version list

Angular 2+

For a version compatible with Angular 2+ versions, please check the sister project a-pattern-restrict.