CautemocSg/ng-ip-address

View on GitHub
ngIpAddress.vanilla.min.js

Summary

Maintainability
A
0 mins
Test Coverage
!function(){"use strict";angular.module("ng-ip-address",[]).directive("ngIpAddress",function(){return{restrict:"A",require:"?ngModel",link:function(e,a,i,u){if(u){var d=new RegExp("^0","g"),l=new RegExp("^\\.","g"),w=new RegExp("\\.\\.+","g"),p=0,n=!1;a.bind("keydown",function(e){17!==e.which&&91!==e.which||(n=!0)}),a.bind("keyup",function(e){17!==e.which&&91!==e.which||(n=!1)}),a.bind("keypress",function(e){(e.which<46&&0!==e.which&&8!==e.which&&13!==e.which||47===e.which||57<e.which&&(!n||99!==e.which&&118!==e.which&&120!==e.which))&&e.preventDefault()}),u.$parsers.push(function(e){if(!e)return u.$setValidity("ipAddress",!0),e;var i=e;p=a[0].selectionStart;var n=!0,r=(i=(i=i.replace(l,"")).replace(w,".")).split("."),t=r.length;t<4?n=!1:4<t&&(r.length=4,t=4);for(var h=0;h<t;h++){var c=r[h];1<c.length?(c=c.replace(d,"").substring(0,3),s=c,(isNaN(parseFloat(s))||!isFinite(s)||255<c)&&(n=!1)):c||(n=!1),r[h]=c}var s;return i=r.join("."),u.$setValidity("ipAddress",n),i===e?e:(u.$setViewValue(i),u.$render(),a[0].setSelectionRange(p,p),i)})}}}})}();