By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,718 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,718 IT Pros & Developers. It's quick & easy.

help with a script

P: n/a
hello!
can somebody clear what does line 2 and line3 mean??
thanx!

function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; this line
if (re.test(ipaddr)) { and
this
var parts = ipaddr.split(".");
if (parseInt(parseFloat(parts[0])) == 0) { return false; }
for (var i=0; i<parts.length; i++) {
if (parseInt(parseFloat(parts[i])) > 255) { return false; }
}
return true;
} else {
return false;
}
}
Nov 24 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
vili wrote on 24 nov 2005 in comp.lang.javascript:
hello!
can somebody clear what does line 2 and line3 mean??
thanx!

function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; //this line
if (re.test(ipaddr)) { //and this
Regular expression,
tries to validate an IP address.
var parts = ipaddr.split("."); if (parseInt(parseFloat(parts[0])) == 0) { return false; }
for (var i=0; i<parts.length; i++) {
if (parseInt(parseFloat(parts[i])) > 255) { return false; }
}
return true;
} else {
return false;
}
}


further validation.
Say, this is terrible coding !
The regex already has certified the parts are integer.
putting an else after a return is alsio not very useful.

See fot theory of regex validation:
http://www.merlyn.demon.co.uk/js-valid.htm

==================

you could try:

function validateIP(ip) {
var ar = ip.split('.');
if (ar.length != 4) return false;
for (var n = 0; n < 4; n++) {
var x = ar[n];
var z = (n == 0)? 1 : 0
if (/\D/.test(x)|| /^0\d/.test(x) || z>x || 255<x)
return false;
}
return true;
}
alert(validateIP("1.0.89.123")) // true
alert(validateIP("1.0.089.123")) //false
alert(validateIP("0.0.89.123")) //false
alert(validateIP("5.0.289.123")) //false
alert(validateIP("5.289.123")) //false
alert(validateIP("5..289.123")) //false

===================

z = (n == 0)? 1 : 0 -> ar[0] not zero

/\D/.test(x) -> only numeric chars allowed

/^0\d/.test(x) -> no leading zeros

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 24 '05 #2

P: n/a
vili wrote:
hello!
can somebody clear what does line 2 and line3 mean??
thanx!

function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; this line
That creates a regular expression called 're' that will match a string
starting with 1 to 3 digits, then a period '.', 1 to 3 digits, then a
period, 1 to 3 digits, then a period, then 1 to 3 digits at the end.

It appears to match the pattern of an IP address, e.g. 192.168.1.1. It
will also match 999.0.80.888 and many others.

if (re.test(ipaddr)) {
That tests if the pattern appears in the value of the variable 'ipaddr'

var parts = ipaddr.split(".");
This will create an array 'parts' that is the elements of ipaddr when
split using a period '.'.

if (parseInt(parseFloat(parts[0])) == 0) { return false; }
That would appear redundant, since the previous test has already
determined that the elements of 'parts' will only consist of digits, and
that any number will be an integer. A simpler test is:

if ( parts[0] == '0') { return false; }

for (var i=0; i<parts.length; i++) {
if (parseInt(parseFloat(parts[i])) > 255) { return false; }
Again, parseInt & parseFloat here are redundant:

if ( parts[i] > 255 ) { return false; }
If you want to be explicit about it, convert parts[i] to a number (but
it isn't necessary):

if ( +parts[i] > 255 ) { return false; }
}
return true;
} else {
return false;
}
}


A more concise (though perhaps more obfuscated) test is:

function isValidIPv4Addr(ipaddr)
{
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
if ( !re.test(ipaddr) ) { return false; }
var parts = ipaddr.split('.');
return (parts[0] != 0 && parts[0] <= 255)
&& parts[1] <= 255
&& parts[2] <= 255
&& parts[3] <= 255;
}
I am no expert on IP addresses, but it seems that '0' is valid as the
first component of the address, I don't know why the test discounts it.

The scheme also only matches an IP version 4 address and will not match
the new IP version 6 addresses[1], so perhaps the function should be
called 'isValidIPv4Address'.

1. <URL: http://en.wikipedia.org/wiki/IPv6 >

--
Rob
Nov 24 '05 #3

P: n/a
JRS: In article <dm**********@sunce.iskon.hr>, dated Thu, 24 Nov 2005
22:51:06, seen in news:comp.lang.javascript, vili
<vi*******@hi.htnet.hr> posted :
can somebody clear what does line 2 and line3 mean?? function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; this line
if (re.test(ipaddr)) { and
this
var parts = ipaddr.split(".");
if (parseInt(parseFloat(parts[0])) == 0) { return false; }
for (var i=0; i<parts.length; i++) {
if (parseInt(parseFloat(parts[i])) > 255) { return false; }
}
return true;
} else {
return false;
}
}

If copying code, try to be more selective in what you choose to copy.
That looks as if it was written by an American.

Please don't let your posting agent wrap code lines / annotation.

Please use an informative Subject line.

Line 2 is setting a variable to a RegExp literal and line 3 is using
that to text whether ipaddr is of the form "###.###.###.###", where each
### represents 1-3 decimal digits.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm> and links for more.
The code can be simplified to this, I think;

function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
if (!re.test(ipaddr)) return false
var parts = ipaddr.split(".")
if (+parts[0] == 0) return false
for (var i=0; i<parts.length; i++) if (+parts[i] > 255) return false
return true }

Your parseInt(parseFloat(x)) is I suppose used to read numbers with
leading zeroes as decimal; the unary + operator will do that, much
faster - see newsgroup FAQ.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 26 '05 #4

P: n/a
Dr John Stockton wrote on 26 nov 2005 in comp.lang.javascript:
Your parseInt(parseFloat(x)) is I suppose used to read numbers with
leading zeroes as decimal; the unary + operator will do that, much
faster - see newsgroup FAQ.


However, I would declare leading zeros illegal in a IP-number.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 26 '05 #5

P: n/a
Dr John Stockton wrote on 26 nov 2005 in comp.lang.javascript:
The code can be simplified to this, I think;

function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
if (!re.test(ipaddr)) return false
var parts = ipaddr.split(".")
if (+parts[0] == 0) return false
for (var i=0; i<parts.length; i++) if (+parts[i] > 255) return false
return true }


However I would prefer an all-regex solution:

function isValidIPAddress(ipaddr) {
var re = /^([1-9]\d{0,1}||1\d{2}||2[0-4]\d||25[0-5])
(\.(\d||[1-9]\d||1\d{2}||2[0-4]\d||25[0-5])){3}$/

// please put the above re = ... on one line

return re.test(ipaddr)
}

ip = '123.255.0.123'
document.write(ip+" is "+isValidIPAddress(ip)+'<br>')
ip = '123.256.0.123'
document.write(ip+" is "+isValidIPAddress(ip))

Partly tested, please do not trust this as final.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 26 '05 #6

P: 1
function check_ipe(s_ipe){
var re_ip = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
if (! re_ip.test(s_ipe))
return false;
if (RegExp.$1 > 255 || RegExp.$2 > 255 || RegExp.$3 > 255 || RegExp.$4 > 255)
return false;
return true;
}
May 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.