【发布时间】:2015-11-02 18:11:56
【问题描述】:
我使用这个模式来检查一个字段的表单是否是一个 IP 地址:
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);
if (IPvalue == "0.0.0.0") {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
} else if (IPvalue == "255.255.255.255") {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
} if (ipArray == null) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
} else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
}
if (thisSegment.toString() == "*")
errorString = "";
}
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.");
else
alert (errorString);
}
}
但我需要考虑具有星号“*”或范围“0-255”的八位字节的字段的值。
例如:
192.168.1.1 --> It will be OK
192.168.*.* --> It will be OK
192.168.2-3.0-128 --> It will be OK
192.168.2-3.* --> It will be OK
有什么想法吗?非常感谢!
【问题讨论】:
-
关于您的标签:设计模式和正则表达式模式彼此无关。不超过你衬衫上的图案。
标签: javascript html regex ip-address