angular / ui-mask ip address

时间:2016-06-28 17:16:48

标签: javascript angularjs mask

我在项目中使用angular / ui-mask。现在我需要一个用于阻止ip地址范围的掩码(例如192.168.70.18/20)。例如:

 <input  ng-model="address" ui-mask="9?9?9.9?9?9.9?9?9.9?9?9/9?9" ui-mask-placeholder-char="_" placeholder="" ng-disabled="disabled">

问题是如此,我只能输入此IP地址:192.168.125.254

jsFiddle:http://jsfiddle.net/Sheinar/Lvc0u55v/6133/

1 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题:

$scope.$watch('address', function (address) {
    if (!address || address.length < 15) return;

    var endFrom = parseInt(address.substr(9, 3)),
        endTo = parseInt(address.substr(12, 3));

    if (endFrom > endTo) {
        alert('invalida range');
        $scope.address = address.substr(0, 12);
    }
});

http://jsfiddle.net/matheusdev/Lvc0u55v/6144/