Text input validation using ng-pattern in AngularJS

706 Views Asked by At

I want to validate a text input with ng-pattern directory in AngularJs. The text input can have multiple IP addresses and IP ranges with , (comma) separated.

for an example;

172.168.21.3, 172.168.45.3/8, 172.53.23.12

Can someone help me on this.

1

There are 1 best solutions below

1
On

why not try something like this:

// controller
$scope.regex = /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),?)/g;

// markup
<input type="text" ng-model="ipAddress" ng-pattern="regex">

That regex should match an IP Address any number of times with a conditional comma on the end, I put together a small plnkr example.