help with a script

V

vili

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)) > 255) { return false; }
}
return true;
} else {
return false;
}
}
 
E

Evertjan.

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)) > 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
 
R

RobG

vili said:
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)) > 255) { return false; }


Again, parseInt & parseFloat here are redundant:

if ( parts > 255 ) { return false; }


If you want to be explicit about it, convert parts to a number (but
it isn't necessary):

if ( +parts > 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 >
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 24 Nov 2005
22:51:06, seen in vili
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)) > 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 > 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.
 
E

Evertjan.

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.
 
E

Evertjan.

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 > 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,107
Latest member
Vinay Kumar Nevatia_
Top