admin管理员组文章数量:1335363
I want to check the range of ip address in a regular expression , I was using this way and it's working so successfully
function validate_ip(ip)
{
// See if x looks like an IP address using our "almost IP regex".
var regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var match = regex.exec(ip);
if (!match) return false;
// Additional code to check that the octets aren't greater than 255:
for (var i = 1; i <= 4; ++i) {
if (parseInt(match[i]) > 255)
return false;
}
return true;
}
now i want to perform checking of the range and the syntax in just regular expression can this be done ?
I want to check the range of ip address in a regular expression , I was using this way and it's working so successfully
function validate_ip(ip)
{
// See if x looks like an IP address using our "almost IP regex".
var regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var match = regex.exec(ip);
if (!match) return false;
// Additional code to check that the octets aren't greater than 255:
for (var i = 1; i <= 4; ++i) {
if (parseInt(match[i]) > 255)
return false;
}
return true;
}
now i want to perform checking of the range and the syntax in just regular expression can this be done ?
Share Improve this question edited Jan 25, 2013 at 14:51 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Jan 24, 2013 at 14:35 SallySally 872 silver badges12 bronze badges 3- 3 Yes, it could be done with regex - but why would you ever do that? – Bergi Commented Jan 24, 2013 at 14:37
- 2 Yes you could, but it would be far from pretty. – phant0m Commented Jan 24, 2013 at 14:38
- I know but I have to check url contains ip 192.168.0.137/sss – Sally Commented Jan 24, 2013 at 14:46
3 Answers
Reset to default 5The most straightforward approach is to look at the different cases:
25[0-5]|2[0-4]\d|1?\d\d?
This will match numbers between 0 and 255, disallowing prefixed zeroes such as: 055.
If you want to exclude zero:
25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?
The regex for digits representing numbers from 1 to 255 would look like this:
/[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]/
// next try, allowing 0:
/[1-9]?\d|1\d\d|2[0-4]\d|25[0-5]/
I think you will admit that using parseInt
is much more readable, less errorprone and better maintainable.
It even could be shorter:
/^\d{1,3}(\.\d{1,3}){3}$/.test(ip) && ip.split(".").every(function(octet) {
return parseInt(octet, 10) < 256;
});
(using ES5 every
method, might need a shim for legacy browsers)
May late but, some one could try:
Example of valid IP address
115.42.150.37
192.168.0.1
110.234.52.124
Example of INVALID IP address
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digit has allowed
255.0.0.y – only digit has allowed
666.10.10.20 – digit must between [0-255]
4444.11.11.11 – digit must between [0-255]
33.3333.33.3 – digit must between [0-255]
JavaScript code to validate an IP address
function ValidateIPaddress(ipaddress)
{
if (/^(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]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress))
{
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
本文标签: regexCheck range of IP address using javascript reqular expression onlyStack Overflow
版权声明:本文标题:regex - Check range of IP address using javascript reqular expression only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243738a2438998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论