Git Product home page Git Product logo

angularjs-character-limit-number-input's Introduction

angularjs-character-limit-number-input

description: number type input directive that limits the character count, and properly handles paste event, it will also focus the submit button when character limit is reached

using best practices according to https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

angular
  .module('app', []);

// // // // // // // // // // // // // // // // // // // //
// //  MAIN CONTROLLER
// // // // // // // // // // // // // // // // // // // //
angular
  .module('app')
  .controller('mainCtrl', mainCtrl);

mainCtrl.$inject = ['$scope'];

function mainCtrl($scope) {
  this.val = 4;
}

// // // // // // // // // // // // // // // // // // // //
// //  NUM-MAX DIRECTIVE
// // // // // // // // // // // // // // // // // // // //

/**
 * @desc number type input directive that limits the character count, and properly handles paste event, it will also focus the submit button when max is reached
 * @example <num-max max="6"></num-max> || <num-max max="ctrl.val-from-controller"></num-max>
 */
angular
  .module('app')
  .directive('numMax', numMax);

mainCtrl.$inject = ['$timeout'];

function numMax($timeout) {
  var directive = {
    scope: {
      limit: '=limit'
    },
    link: link,
    template: '<input type="number" min="0">',
    restrict: 'EA',
    replace: true
  };
  return directive;

  function link(scope, element, attrs) {
    element.on('keydown keyup', handleInput);
    element.on('paste', handlePaste);

    var eLength,
      sub,
      txt;

    function handleInput(e) {
      eLength = element[0].value.length;
      sub = e.target.parentElement.lastElementChild;
      txt = /Backspace|Enter|Tab|Delete|ArrowLeft|ArrowRight|Control|a/;

      if (!element[0].value) element[0].value = "";
      if (e.keycode > 47 && e.keycode < 58) {
        if (eLength >= scope.limit) e.preventDefault();
      } else if (!txt.test(event.key)) {
        if (eLength >= scope.limit) {
          e.preventDefault();
          sub.focus();
        }
      }

    }

    function handlePaste(e) {
      sub = e.target.parentElement.lastElementChild;

      $timeout(function() {
        eLength = element[0].value.length;

        if (!element[0].value) element[0].value = "";
        else if (eLength >= scope.limit) {
          element[0].value = element[0].value.slice(0, scope.limit);
          sub.focus();
        }

      }, 0);
    }

  }

}

angularjs-character-limit-number-input's People

Contributors

msorce avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.