473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help with a script

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
6 1534
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
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
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
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
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
bunker
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
4
by: Derek | last post by:
I have the following script in a page and it gets an error in IE 6. Says something about an invalid argument but the line number doesn't help since I can't see the javascript code when viewing...
7
by: mike | last post by:
Hello, I am kind of new to this javascript stuff and I am constantly having problems trying to get my webpage validated. I have the following <script>printdate();</script> and when I validate it...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.