How to validate an IP in a textfield in AngularJS ? Currently I am using this code, but its not working in all cases . Any idea ?
如何在AngularJS的textfield中验证IP ?目前我正在使用这段代码,但它在所有情况下都不起作用。任何想法?
ng-pattern='/^((([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$/
ng-pattern = ' / ^((((01)?[0 - 9]{ 1,2 })|(2[0 - 9][0 - 4])|(25[0]))[]){ 3 }(((0 - 1)?[0 - 9]{ 1,2 })|(2[0 - 9][0 - 4])|(25[0]))/美元
1 个解决方案
#1
9
Use:
使用:
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
Matches 0.0.0.0 through 255.255.255.255
匹配通过255.255.255.255 0.0.0.0
If you want to drop 0.0.0.0
and255.255.255.255
I suggest you to add additional if
statement. Otherwise the regex will be too complicated.
如果你想降低0.0.0.0.0.0和255.255.255.255,我建议你增加If语句。否则,regex将过于复杂。
Example with watcher:
与观察者的例子:
$scope.ip = '1.2.3.4';
$scope.$watch(function () {
return $scope.ip;
},
function (newVal, oldVal) {
if (
newVal != '0.0.0.0' && newVal != '255.255.255.255' &&
newVal.match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/))
{
// Match attempt succeeded
} else {
// Match attempt failed
}
})
Demo Fiddle
演示小提琴
[Edit]
(编辑)
The best bet is to create directive, something like: <ip-input>
最好的方法是创建指令,比如:
This example might helpful how to create directive for custom input: format-input-value-in-angularjs
这个示例可能有助于为定制输入创建指令:format-input-value-in-angularjs
#1
9
Use:
使用:
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
Matches 0.0.0.0 through 255.255.255.255
匹配通过255.255.255.255 0.0.0.0
If you want to drop 0.0.0.0
and255.255.255.255
I suggest you to add additional if
statement. Otherwise the regex will be too complicated.
如果你想降低0.0.0.0.0.0和255.255.255.255,我建议你增加If语句。否则,regex将过于复杂。
Example with watcher:
与观察者的例子:
$scope.ip = '1.2.3.4';
$scope.$watch(function () {
return $scope.ip;
},
function (newVal, oldVal) {
if (
newVal != '0.0.0.0' && newVal != '255.255.255.255' &&
newVal.match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/))
{
// Match attempt succeeded
} else {
// Match attempt failed
}
})
Demo Fiddle
演示小提琴
[Edit]
(编辑)
The best bet is to create directive, something like: <ip-input>
最好的方法是创建指令,比如:
This example might helpful how to create directive for custom input: format-input-value-in-angularjs
这个示例可能有助于为定制输入创建指令:format-input-value-in-angularjs