npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-pattern-restrict

v0.2.3

Published

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

Downloads

11,866

Readme

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:

  • Initialization: save regex to be used from the ng-pattern-restrict attribute or the pattern attribute.
  • Wait for user input, and reach on input (for any value change), keyup (for keyboard entry) or click (for text drag and drop, contextual copy-paste, etc).
  • If the input matches the regex, save it as the latest valid value. Also save the current user's input caret position on the field.
  • If the input does not match the regex, restore the latest valid value and set the user input caret in the same position as it was. The user experience should be as if nothing was typed or changed.

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, load it into the browser and use it away.

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]+" />

Notes:

  • (#15) Make sure to use "progressive" regular expressions. The expressions are validated against the full regular expression. This means that if your regex is \d\d, and your textbox is empty, a user will never be able to type anything because the first keypress will not validate and will be reverted. Following the example, the proper regular expression should be \d{0,2}.
  • Your regex must validate against empty strings if you want users to be able to remove the value from the textbox.
  • Make sure AngularJS is compatible with your browser. It goes without saying, but your browser compatibility with AngularJS will make a huge impact on how well this directive can behave. For example, AngularJS 1.3 with IE 8 won't really play well together, and this directive is restricted by those limitations.

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